Hi Jenda, That was amazing..... It was a good learning for me, and I never knew about Benchmark...
Sorry for that... My reasoning was absolutely logical and nothing systematical.... Thanks for the great reasoning which was real real systematical. But the same script in my environment gives me the following result: Benchmark: timing 100 iterations of withAry, withAryAnd, withIf, withIfAnd, withQm, withQmAnd... withAry: 18 wallclock secs (17.60 usr + 0.01 sys = 17.61 CPU) @ 5.68/s (n=100) withAryAnd: 17 wallclock secs (17.37 usr + 0.01 sys = 17.38 CPU) @ 5.75/s (n=100) withIf: 20 wallclock secs (19.34 usr + 0.00 sys = 19.34 CPU) @ 5.17/s (n=100) withIfAnd: 19 wallclock secs (19.07 usr + 0.00 sys = 19.07 CPU) @ 5.24/s (n=100) withQm: 16 wallclock secs (16.42 usr + 0.01 sys = 16.43 CPU) @ 6.09/s (n=100) withQmAnd: 17 wallclock secs (16.10 usr + 0.00 sys = 16.10 CPU) @ 6.21/s (n=100) Well..... This is HP-UX 9000/785......... Now I really wonder how consistent the Benchmark gives the result properly..... bcoz different runs gave different results... Fine anyhow I should admit that using array is slower than (or as fast as) using ?:, but it is faster than using "if...else" both in your environment and in mine (even for several runs). Anyways thanks for letting me know a good Perl Module.... Benchmark.pm With Best regards, R. Kamal Raj Guptha. -----Original Message----- From: Jenda Krynicky [mailto:[EMAIL PROTECTED] Sent: Tuesday, August 03, 2004 8:00 PM To: R. Kamal Raj Guptha (WT01 - TELECOM & INTER-NETWORKING SOLUTIONS); [EMAIL PROTECTED] Subject: RE: Determining Odd and Even Numbers From: <[EMAIL PROTECTED]> > Well, > > All the above said answers are perfectly fine, but just to make it > more efficient what we can do is, we can avoid the if statement or ?: > operator. > > This is because whenever we go for a decision statement, it takes some > extra processing time. Whenever we index an array it takes some extra processing time as well. > A solution to avoid using if or ?: is as follows: > > #!/usr/bin/perl > use strict; > > my @string = ("Even", "Odd"); > print $string[$ARGV[0]%2]; > > Well it might look like the savings in time is less, but if you > consider that this code is inside a loop which will run for several > iterations, you are sacing lot of time.... Hence the program becomes > more efficient. Before you say something like this you should test it: use Benchmark; sub withIf { my $number_is = ''; for (1..100000) { if ($_ % 2) { $number_is = 'Odd'; } else { $number_is = 'Even'; } } } sub withQm { my $number_is = ''; for (1..100000) { $number_is = ($_ % 2) ? 'Odd' : 'Even'; } } sub withIfAnd { my $number_is = ''; for (1..100000) { if ($_ & 1) { $number_is = 'Odd'; } else { $number_is = 'Even'; } } } sub withQmAnd { my $number_is = ''; for (1..100000) { $number_is = ($_ & 1) ? 'Odd' : 'Even'; } } my @what = qw(Even Odd); sub withAry { my $number_is = ''; for (1..100000) { $number_is = $what[$_ % 2]; } } sub withAryAnd { my $number_is = ''; for (1..100000) { $number_is = $what[$_ & 1]; } } timethese 100, { withIf => \&withIf, withQm => \&withQm, withIfAnd => \&withIfAnd, withQmAnd => \&withQmAnd, withAry => \&withAry, withAryAnd => \&withAryAnd, } Benchmark: timing 100 iterations of withAry, withAryAnd, withIf, withIfAnd, withQm, withQmAnd... withAry: 6 wallclock secs ( 5.67 usr + 0.00 sys = 5.67 CPU) @ 17.63/s (n=100) withAryAnd: 5 wallclock secs ( 5.20 usr + 0.02 sys = 5.22 CPU) @ 19.16/s (n=100) withIf: 6 wallclock secs ( 6.09 usr + 0.01 sys = 6.11 CPU) @ 16.37/s (n=100) withIfAnd: 6 wallclock secs ( 5.77 usr + 0.00 sys = 5.77 CPU) @ 17.35/s (n=100) withQm: 5 wallclock secs ( 5.05 usr + 0.00 sys = 5.05 CPU) @ 19.81/s (n=100) withQmAnd: 5 wallclock secs ( 4.73 usr + 0.00 sys = 4.73 CPU) @ 21.12/s (n=100) (Using Perl v5.8.0 ActiveState build 805 on Win2k server) So it seems the ?: is actually fastest ;-) Jenda ===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz ===== When it comes to wine, women and song, wizards are allowed to get drunk and croon as much as they like. -- Terry Pratchett in Sourcery -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response> Confidentiality Notice The information contained in this electronic message and any attachments to this message are intended for the exclusive use of the addressee(s) and may contain confidential or privileged information. If you are not the intended recipient, please notify the sender at Wipro or [EMAIL PROTECTED] immediately and destroy all copies of this message and any attachments. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>