>>>>> "XL" == Xi Liu <[email protected]> writes:
XL> I know I am doing the repetitive and useless summing again and again. What
XL> confuses me is that using the same algorithm, I mean
XL> for my $i (99 .. 60000)
XL> {
XL> my $sum;
XL> map {$sum += $_->{foo_value}} @lines[$i - $n + 1 .. $i];
XL> push @res, $sum;
XL> }
XL> in perl and
XL> for(int i =99; i <= 60000; i++)
XL> {
XL> int sum = 0;
XL> for(int j = i - n +1; j < i; j++)
XL> sum += lines[$j].foo_value;
XL> res[i] = sum;
XL> }
XL> in c, there is a huge difference in efficiency. the c program,
XL> even using the same stupid algorithm, the cost of time is
XL> acceptable. So I translated it to perl , and avoid the slow
XL> subscripting operations using map on sliced array, I suppose the
XL> perl program would be slower, but I don't predict such a huge
XL> difference, you don't even need a benchmark or profiling tool to
XL> notice the efficiency difference. This is what
nothing to be confused about. this is the difference between a compiled
and interpreted language. the c code you have is compiled down to very
efficient machine code. a structure access in c is very cheap (just an
addition or so in machine code). a hash access in perl is much more
expensive. general perl code will never be close to the speed of
compiled code. specialized perl code using its guts like the regex
engine can be close to c code especially badly written code. the win for
perl is the much faster time coding up the program and the flexibility
of perl over c. i did over 20 years of c and spent half my time doing
stuff that perl does for me like memory management, manipulating data
structures, dynamic data issues, etc. my time is more valuable than the
computer's so i choose perl over c. this difference is well known by
anyone who deals with c and perl. it is why you can write c code inside
perl (with XS, Inline::C, etc). many cpan modules have c code in them
for speedup. many existing libraries are all in c and perl modules
provide wrappers for them. c is not going away and neither is perl. if
you want blazing speed on simple stuff like your code, keep it in
c. perl will never be fast enough for that.
uri
--
Uri Guttman ------ [email protected] -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/