Hi matt! On Wednesday 02 Dec 2009 18:29:53 matt wrote: > On Dec 1, 8:58 pm, practicalp...@gmail.com wrote: > > Hello, > > > > Maybe it's not so suitable to ask this here. > > But is there a good way (code sample?) to implement a speed test > > between Perl and C? > > For a project which handles lots of data we want to know how slower > > perl is than C. > > > > Thanks. > > To perform the test, I'd just use 'time': > > test.cc: > int main() > { > int a = 1 + 1; > } > > test.pl: > #!/sw/bin/perl > $a = 1 + 1; >
Just a few notes: In both of these cases, the "1+1" expression should be evaluated at compile- time by the compilers, because it contains nothing but constants. Furthermore, g++ may be smart enough to see that you're not doing anything with "a" and to do nothing in this case. A more representative example would be one that inputs two integrarl arguments (from argv/@ARGV or STDIN) and adds them in run-time. If you're looking for something a bit more larger in scope, see: http://github.com/shlomif/Project-Euler-Problem-10-Comparison I've written similar programs in C, Haskell, and Perl to compare Problem #10 in Project Euler. The results were: <<<<<<<<<<<<<<< # This was ran on: # * A Pentium 4 2.4 GHz machine with 2.5 GB of RAM # * Running Mandriva Linux Cooker (as of 28-November-2009) # * On top of XFCE from Cooker and very few things running in the background. Benchmark: timing 500 iterations of c_mine, c_mine_half, c_mine_micro_opt... c_mine: 15 wallclock secs ( 0.02 usr 0.13 sys + 12.20 cusr 2.37 csys = 14.72 CPU) @ 33.97/s (n=500) c_mine_half: 10 wallclock secs ( 0.02 usr 0.13 sys + 6.75 cusr 2.30 csys = 9.20 CPU) @ 54.35/s (n=500) c_mine_micro_opt: 15 wallclock secs ( 0.02 usr 0.13 sys + 11.34 cusr 2.36 csys = 13.85 CPU) @ 36.10/s (n=500) Benchmark: timing 20 iterations of haskell_mine, haskell_zeroth, perl-10, perl-10_half... haskell_mine: 135 wallclock secs ( 0.00 usr 0.00 sys + 134.71 cusr 0.70 csys = 135.41 CPU) @ 0.15/s (n=20) haskell_zeroth: 310 wallclock secs ( 0.00 usr 0.01 sys + 307.61 cusr 1.15 csys = 308.77 CPU) @ 0.06/s (n=20) perl-10: 95 wallclock secs ( 0.00 usr 0.01 sys + 94.14 cusr 0.30 csys = 94.45 CPU) @ 0.21/s (n=20) perl-10_half: 73 wallclock secs ( 0.00 usr 0.00 sys + 71.34 cusr 0.36 csys = 71.70 CPU) @ 0.28/s (n=20) >>>>>>>>>>>>>>> So the Perl Benchmark.pm module (with some overhead) executed the fastest C program 54.35 times per second, and the fastest Perl program at 0.28 times per second - roughly 194.1 times faster. The Haskell program was even worse at 0.15 times per second. I should note that the Haskell program used a different algorithm, but that both algorithm are functionally equivalent, and that the algorithm I used for C and Perl used an array / vector of bits and as a result would likely perform much poorly in Haskell. > # time bash -c 'for x in {1..100}; do ./a.out; done' > > real 0m0.167s > user 0m0.029s > sys 0m0.138s > > # time bash -c 'for x in {1..100}; do ./test2.pl; done' > > real 0m0.435s > user 0m0.154s > sys 0m0.247s > > So...I've proved that in my specific environment, C is (~3x) faster > than Perl at adding 1+1...Now as far as what tests you want to > implement, that's up to you and your specific needs. > One should note that there's also the overhead of the bash loop here. Regards, Shlomi Fish -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ Optimising Code for Speed - http://shlom.in/optimise Chuck Norris read the entire English Wikipedia in 24 hours. Twice. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/