Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Re: Re: Module benchmarks...

by ariels (Curate)
on May 02, 2002 at 13:18 UTC ( [id://163530]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Module benchmarks...
in thread Module benchmarks...

It probably depends on what you're trying to do, and how important speed is to you. I just checked (using Inline), and calculating integer factorials is anywhere from 7x to 15x faster in C than in Perl.

If most of what my program did was compute factorials, I'd code them in C. If it were 1% of execution time, I'd code it in Perl, optimizing for maintainability. If most of what your program does is compute dates, optimize by using the fastest available module (or just "a faster" module). If most of what your program does is other stuff, optimize for readability by picking the module with the most elegant interface, and possibly the most maintainable code.

That said, it's certainly a big difference; it pays to know that.


#!/usr/local/bin/perl -w use strict; use Benchmark; use Inline C => <<'END_OF_C'; int factorial_c(int n) { int i, result; for(result=1, i=2; i<=n; i++) result *= i; return result; } END_OF_C sub factorial_perl { my $n = shift; my $res=1; my $i; for($i=2; $i<=$n; $i++) { $res *= $i } $res; } factorial_perl(10) == factorial_c(10) or die "differing values, stopped"; my $res; timethese(4_000_000, { C => '$res = factorial_c(10)', Perl => '$res = factorial_perl(10)', } );

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://163530]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-24 08:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found