One for the Perl Wizards... 
Author Message
 One for the Perl Wizards...

In the following piece of code, any idea why the index() call, which
is outside the while loop, slows down the while loop? For some reason,
using the index() function decreases program performance, even if its
called only once outside any looping block...

use constant VERBOSE => scalar 500000;
open(DATA, "Version1-1/IB.dat");
open(OUTPUT_FILE, ">test.dat");
$tm = `date`; chomp($tm); print STDERR "[$tm] $. records.\n";
$s = index($tm, 0, 3);
while (<DATA>)
{
    print OUTPUT_FILE;
    next unless ($. % VERBOSE == 0);
    $tm = `date`; chomp($tm); print STDERR "[$tm] $. records.\n";

Quote:
}

Thanks, Mario


Fri, 23 Apr 2004 06:30:11 GMT  
 One for the Perl Wizards...

Quote:

>In the following piece of code, any idea why the index() call, which
>is outside the while loop, slows down the while loop?

Can't help with that, but your code has some other cruft that
I'll comment on.

Quote:
>use constant VERBOSE => scalar 500000;
>open(DATA, "Version1-1/IB.dat");

You should always, yes *always*, check the return value from open():

   open(DATA, 'Version1-1/IB.dat') or
      die "could not open 'Version1-1/IB.dat' $!";

Quote:
>$tm = `date`; chomp($tm); print STDERR "[$tm] $. records.\n";

Why shell out for something easily done with native Perl?

   my $tm = localtime;

Quote:
>$s = index($tm, 0, 3);

A string should *look like* a string:

   $s = index($tm, '0', 3);

You should probably be checking the return value here too.

Maybe you meant to use substr() instead of index() ?

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Fri, 23 Apr 2004 15:18:10 GMT  
 One for the Perl Wizards...
On 4 Nov 2001 21:30:11 -0800,

Quote:
> In the following piece of code, any idea why the index() call, which
> is outside the while loop, slows down the while loop? For some reason,
> using the index() function decreases program performance, even if its
> called only once outside any looping block...

For me it makes no significant difference (5.6.1 on i686-linux). The
running time is approximately equal with or without the line with index.
What's your version of Perl and OS?

A few comments:

It is very, very useful when you're debugging problems to make sure to
use strict, and enable warnings. I _know_ you didn't do that.

Quote:
> use constant VERBOSE => scalar 500000;

What is that scalar for there? it doesn't do anything.

Quote:
> open(DATA, "Version1-1/IB.dat");
> open(OUTPUT_FILE, ">test.dat");

Always check the result of an open:

open(DATA, "Version1-1/IB.dat") or die "Cannot open Version1-1/IB.dat: $!";

Quote:
> $tm = `date`; chomp($tm); print STDERR "[$tm] $. records.\n";

Don't put multiple comments on one line.
Don't use external programs when an internal command exists.
    external programs make your code less portable, and waste resources.
$. is undefined at this point.

    my $tm = localtime;
    print STDERR "[$tm]\n";

Quote:
> $s = index($tm, 0, 3);

index() works on strings, not numbers.

$s = index $tm, "0", 3;

And you do realise that 3 is a position which isn't inside the string
you gave it, right?

Quote:
> while (<DATA>)
> {
>     print OUTPUT_FILE;
>     next unless ($. % VERBOSE == 0);
>     $tm = `date`; chomp($tm); print STDERR "[$tm] $. records.\n";

See above.

Quote:
> }

None of these remarks could cause or cure the problem you're seeing,
_if_ you are actually seeing a problem. How did you measure this? Maybe
you should have a look at the Benchmark module so that you can create
some numbers instead of a vague qualification like "slows down the while
loop".

Martien
--
                                |
Martien Verbruggen              | Make it idiot proof and someone will
                                | make a better idiot.
                                |



Fri, 23 Apr 2004 22:47:00 GMT  
 One for the Perl Wizards...

Quote:

>Don't put multiple comments on one line.

   s/comments/statements/;  # is what I'm sure Martien meant

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Sat, 24 Apr 2004 02:48:17 GMT  
 One for the Perl Wizards...
On Tue, 06 Nov 2001 01:48:17 GMT,

Quote:

>>Don't put multiple comments on one line.

>    s/comments/statements/;  # is what I'm sure Martien meant

Indeed. Thanks.

Martien
--
                                |
Martien Verbruggen              | System Administration is a dirty job,
                                | but someone said I had to do it.
                                |



Sat, 24 Apr 2004 05:14:29 GMT  
 One for the Perl Wizards...

Quote:

> On Tue, 06 Nov 2001 01:48:17 GMT,


> >>Don't put multiple comments on one line.

> >    s/comments/statements/;  # is what I'm sure Martien meant

> Indeed. Thanks.

> Martien

I must have been imagining! but I cannot recreate the problem now. All
i can think might have happened is that during my previous testing the
system (dec-osf1) was low on memory resources and using the index()
function may have caused the perl interpreter to run 'differently'.
Thanks all for the usefull hints.
Mario


Sun, 25 Apr 2004 05:22:06 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. RFQ: Tk::Wizard - a wizard style widget for Perl/Tk

2. RFQ: Tk::Wizard - a wizard style widget for Perl/Tk

3. A question to Perl wizards

4. A call to PERL wizards

5. Perl Wizard Quiz

6. Perl for Netware wizards out there?

7. Any CGI/Perl Wizards in Utah?

8. Perl Wizards

9. HELP: (please help, wizards) Memory problems

10. Defining a wizard (was: Okay...)

11. easy XS question for a wizard from a newbie

 

 
Powered by phpBB® Forum Software