develooper Front page | perl.perl5.porters | Postings from December 2003

question about multiple require's

Thread Next
From:
Tels
Date:
December 22, 2003 11:31
Subject:
question about multiple require's
Message ID:
200312222032.05043@bloodgate.com
-----BEGIN PGP SIGNED MESSAGE-----

Moin,

I shuffled around some code and now BigInt requires part of itself when 
neccessary. The code to do this looks like this:

 require $EMU_LIB unless $EMU_LOADED; $EMU_LOADED = 1;

I wondered whether it is safe to just write require (for older Perl version?), 
or even faster. The goal was to never load the file twice.

Benchmarking it:

te@null:~/perl/math/Math-BigInt-1.68/lib> perl req.pl
Benchmark: running req, req1 for at least 4 CPU seconds...
       req:  6 ( 4.89 usr +  0.00 sys =  4.89 CPU) @ 5056941/s (n=24728445)
      req1:  3 ( 4.19 usr +  0.00 sys =  4.19 CPU) @ 3503592/s (n=14680054)
te@null:~/perl/math/Math-BigInt-1.68/lib> cat req.pl
#!/usr/bin/perl -w

use Benchmark; use Math::BigInt;

my $EMU_LIB = 'Math/BigInt/CalcEmu.pm'; # emulate low-level math
my $EMU_LOADED = 0;                     # already required?

# require it now to make conditions equal for both versions
require $EMU_LIB; $EMU_LOADED = 1;

timethese (-4,
  {
  req => sub { require $EMU_LIB unless $EMU_LOADED; $EMU_LOADED = 1; },
  req1 => sub{ require $EMU_LIB; }
  });

So, it seems my construct is slightly faster. Should I use it, or just plain 
require (that would be shorter to write and save a global flag). I think the 
time spent in require is nothing compared to the time spent in the code that 
follows it (BigInt emulation code), so the speed is probably a theoretical 
issue.

Best wishes,

Tels

- -- 
 Signed on Mon Dec 22 20:27:13 2003 with key 0x93B84C15.
 Visit my photo gallery at http://bloodgate.com/photos/
 PGP key on http://bloodgate.com/tels.asc or per email.

 "Not King yet."

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2-rc1-SuSE (GNU/Linux)
Comment: When cryptography is outlawed, bayl bhgynjf jvyy unir cevinpl.

iQEVAwUBP+dGsncLPEOTuEwVAQG4nwf/Urn0YO5tpH+pz29YcH55edKOG9k0xJ56
FUYMM64wrrnKDnveY6p2kchU0PA8yIxPZjbov+PSXzgBuMtCQ16USs0zRQ695jAk
MeDlbC9O9sEG8vO6kXOP4KqOYTRDwYZ5IByv6pbwxImPdpIUZNyaQkq5wVRgtfuy
PeOz3lHYW9Jq/2dew19LeWbJqEaksNkGNyo1MGD9AjtQa+bSlRyY2C6rFijdNjds
JN8HprzamhzsPG1tR9/0hxlN9b6gbIxy3jtC6vDjLA9u8pJb7Ads9VofNH6hQ3/U
FZIarTKhrAQVyJj8fLrgvKlEA4q46eJDUiI9jzG13hVzjyYS+4mzJQ==
=6DfP
-----END PGP SIGNATURE-----


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About