Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re(2): Lesson Taught

by shotgunefx (Parson)
on Feb 23, 2003 at 01:30 UTC ( #237830=note: print w/replies, xml ) Need Help??

in reply to Re: Teach him a lesson with facts
in thread Teach him a lesson with facts

While I didn't bother to bench the C and Java versions, I can tell you there are many ways to speed this up.

Changing $i to a lexical gives you 10% more speed.

By using the pragma use integer in a lexically scoped block around the code you get 310% more speed.

It may not be a perfect number as I didn't not run the Java and C code, and there may be differences in implementation between my system and yours, but that would bring Perl down to 8 seconds which is a negligible difference.

More importantly, runtime is only a fraction of what matters in a real software project so comparing numbers like this is a really just mental wanking. And the fact that you go to a perl site to do it just says to me that your instigating which is pretty dumb. Would you go to an ethnic neighborhood and start slandering the locals? Not if you weren't looking for trouble.
#!/usr/bin/perl use Benchmark qw(cmpthese timethese); sub iterate { open(FILE, ">", "file.txt"); for ($i = 0; $i < 1000000; $i ++) { print FILE $i++; } close(FILE); } sub lex_iterate { open(FILE, ">", "file.txt"); for (my $i = 0; $i < 1000000; $i ++) { print FILE $i++; } close(FILE); } sub int_iterate { use integer; open(FILE, ">", "file.txt"); for (my $i = 0; $i < 1000000; $i ++) { print FILE $i++; } close(FILE); } my $results = timethese(5 , { iterate=>\&iterate , lex_iterate=>\&lex_ +iterate, int_iterate=>\&int_iterate } ); cmpthese($results); __END__ Benchmark: timing 5 iterations of int_iterate, iterate, lex_iterate... int_iterate: 4 wallclock secs ( 4.03 usr + 0.06 sys = 4.09 CPU) @ +1.22/s (n=5) iterate: 17 wallclock secs (16.70 usr + 0.07 sys = 16.77 CPU) @ 0 +.30/s (n=5) lex_iterate: 15 wallclock secs (15.20 usr + 0.08 sys = 15.28 CPU) @ +0.33/s (n=5) s/iter iterate lex_iterate int_iterate iterate 3.35 -- -9% -76% lex_iterate 3.06 10% -- -73% int_iterate 0.818 310% 2


"To be civilized is to deny one's nature."

Replies are listed 'Best First'.
Re: Re(2): Lesson Taught
by Anonymous Monk on Feb 23, 2003 at 06:20 UTC
    Good one, man. my $i does speed it up, now the ratio become perl:java:c = 27:7:5.

    But use integer does not seem to affect the result, and it should not affect the result on a 32 bit machine, when integers are less than 10^6.

    I also tried 0..1,000,000 as one post suggested, it slows down the whole thing.

    I then became interested in the difference between your benchmark and my result. (I didn't suspect you cook the numbers and I don't, never, just as you should not suspect me.) So I ran your benchmark testing, and got:
    Benchmark: timing 5 iterations of int_iterate, iterate, lex_iterate... int_iterate: 131 wallclock secs (130.94 usr + 0.00 sys = 130.94 CPU) +@ 0.04/s (n=5) iterate: 172 wallclock secs (172.63 usr + 0.00 sys = 172.63 CPU) @ + 0.03/s ( n=5) lex_iterate: 137 wallclock secs (136.66 usr + 0.00 sys = 136.66 CPU) +@ 0.04/s (n=5) s/iter iterate lex_iterate int_iterate iterate 34.5 -- -21% -24% lex_iterate 27.3 26% -- -4% int_iterate 26.2 32% 4% --
    I would believe this is due to my small mem config as I said in another reply.

    But your data shows use integer speeds up the thing a lot, that part really confuses me. I don't think you are using 16 bit OS though. Did you compiled your perl yourself?

      The first benchmark was from a linux 2.2 kernal running 5.6.0 with an Intel Celeron 400MHZ with 256MB.

      On a ultrasparc 10 running linux 2.2 with 256 MB (5.6.1) I get similar results...
      Benchmark: timing 5 iterations of int_iterate, iterate, lex_iterate... int_iterate: 14 wallclock secs (14.19 usr + 0.12 sys = 14.31 CPU) @ +0.35/s (n=5) iterate: 53 wallclock secs (52.45 usr + 0.15 sys = 52.60 CPU) @ 0 +.10/s (n=5) lex_iterate: 47 wallclock secs (47.03 usr + 0.11 sys = 47.14 CPU) @ +0.11/s (n=5) s/iter iterate lex_iterate int_iterate iterate 10.5 -- -10% -73% lex_iterate 9.43 12% -- -70% int_iterate 2.86 268% 229% --
      Regardless... what does this actually mean? Not much. The iteration test isn't usually what the problem is. Usually it's all the stuff in-between that is the bottleneck and how fast the turn-around time is. It to me is a useless metric... (unless you're writing a video driver or such.) I think it's a case of not seeing the forest through the trees. If I was making a renderer or such that needed low-level speed I would just use XS or Inline, but the biggest thing for me is, how quick can I get a job done and how quick can I implement changes and to me Perl fits the bill perfectly.


      "To be civilized is to deny one's nature."
        I believe your results, but my PC does floating really quick, and as you can see it is that much close to integer, which I believe it is doing floating using hardware not software, because I have such hardware installed.

        Just to quickly point out something. The loop is not my purpose either. The original purpose to have the loop there is to make the testing result comparable.

        If I don't have the loop there, all three result would be 0.

        Loop is just a way to help gather results, just as what timthese and cmpthese do. This part actually has been misunderstood. And we both understand/saw that, online communities lots of times follow emotion more than logic.

        If we are talking face to face, we will do it slowly and carefully. But when you do it online, especially a community like this one, where lots of people weigh XP point too much, they started to jump on each other quickly and hit the most soft part of each other's body, as long as it gives them XP point, they don't really mind what they are actually talking.

        I am not pointing to you, absolutely not. I say this to you, simply because I see you as someone reasonable.

        I see the XP point system as really harmful, and it makes lots of people mentally sick.
      Take back my comment on "use integer", I confused it with bigint.

      This might have something to do with our hardware difference, i.e. your machine may not have floating point hardware.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (10)
As of 2018-10-15 21:52 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (82 votes). Check out past polls.