Optimising Perl with Inline::C
I was discussing with someone today about a time I used Inline::C to massively speed up an inner loop in a Perl program. Thing is, in that case the real speedup wasn't any super smart C programming on my behalf, it was just making use of a very optimised vendor library that you could only access from C.
So I got to thinking - in normal every day code, is there any real speed benefit to be had by writing your inner loops in C. I found an old web page by Mitchell Charity discussing Inline and, interpreting his (slightly pathological) example a little, I got a surprising result:
use strict; use warnings; use Inline 'C'; use Benchmark qw(cmpthese); cmpthese( 50, { perl_method => sub { my $object = new Foo; for(my $i=0;$i<1_000_000;$i++) { $object->set_element($i,67); } }, all_in_one_c => sub { my $object = new Foo; set_all_with_c($object); } }); package Foo; sub new { my $self = " " x 1_000_000; return bless \$self, 'Foo' } sub set_element { my($self,$n,$value) = @_; substr($$self,$n,1) = pack("C",$value); } __END__ __C__ #define USING(object) unsigned char *ptr = SvPVX(SvRV(object)) #define SET_ELEMENT(IDX,VAL) ptr[IDX] = VAL void set_all_with_c (SV* object) { USING(object); int i; for(i=0;i<1000000;i++) { SET_ELEMENT(i, 67); } }
s/iter perl_method all_in_one_c
perl_method 4.16 -- -100%
all_in_one_c 8.60e-03 48321% --
ie. the C code was 48321% times as fast.But it's not really comparing apples with apples - the Perl code is doing a method call on each iteration, the C code is operating on the value directly. In addition, the C code (by way of Inline::C's magic) is basically copying the string into a temporary variable, operating directly on that, and copying back - so the dereferencing is not happening on each loop. We can make those changes in Perl too, and see how that compares.
... sub set_all_with_perl { my $tmp_str = ${ $_[0] }; substr($tmp_str, $_, 1) = pack("C",67) for 0..1_000_000; ${ $_[0] } = $tmp_str; } ... s/iter perl_method all_in_one_perl all_in_one_c perl_method 4.21 -- -62% -100% all_in_one_perl 1.61 161% -- -99% all_in_one_c 8.60e-03 48821% 18626% --So eliminating the method dispatch and dereference in the loop made our Perl code much faster, but the C code is still way faster. Obviously it's a contrived example, and 1 million iterations is one heck of an inner loop, but I am still surprised by how much difference it made.
01:38 AM, 11 Jan 2008 by Mark Aufflick Permalink | Short Link | Comments (0) |
Archive
June 2010 May 2010 April 2010 March 2010 February 2010 January 2010 October 2009 September 2009 August 2009 July 2009 June 2009 May 2009 April 2009 February 2009 January 2009 December 2008 November 2008 October 2008 September 2008 August 2008 July 2008 June 2008 May 2008 March 2008 February 2008 January 2008 December 2007 November 2007 October 2007 September 2007 August 2007 July 2007 June 2007 May 2007 April 2007 March 2007 February 2007 January 2007 December 2006 November 2006 October 2006 September 2006 August 2006 July 2006 June 2006 May 2006 April 2006 March 2006 February 2006 January 2006 December 2005 November 2005 October 2005 September 2005 August 2005 July 2005 June 2005 May 2005 April 2005 March 2005 February 2005 January 2005 December 2004 November 2004 October 2004 September 2004 August 2004 July 2004 June 2004 May 2004 April 2004 March 2004 February 2004 January 2004 December 2003 November 2003 October 2003 September 2003 August 2003 Blog Categoriessoftware (27)..cocoa (13) ..heads up 'tunes (5) ..ruby (4) ..lisp (1) ..perl (3) ..openacs (1) mac (18) embedded (2) ..microprocessor (2) ..avr (1) electronics (3) design (1) photography (24) ..black and white (6) ..A day in Sydney (18) ..The Daily Shoot (5) food (2) Notifications Request notifications
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||








Request notifications