in reply to Benchmark.pm's scoping behavior
That's not doing what you think it's doing. That code is accessing $R not because you've specified "package R" but because $R is lexically-scoped to the containing file. If you had been running your quasi-benchmark code in a different file, it wouldn't have worked. That's why, in your original code, the benchmarked code couldn't access $now and %url when you declared them with my: because they were declared lexical to the enclosing file, and your code was running in a different file.package R; my $R = 10; package main; my $subcode = "sub { local \$_; package R; print $R}"; my $subref = eval $subcode; &$subref;
That's why you need to make your variables package globals, presumably using the vars pragma, but probably you could also use our, in Perl 5.6. Because those package globals are in your package's namespace, so when Benchmark executes your code in the "caller's package", it's accessing the correct variables.
As for your other question:
I don't know, but I think lhoward had the best suggestion: the sort may be O(N log N), but that's not the only measure of efficiency; how you code the algorithm, and how it's optimized, is also a big influence on speed. So running an O(N log N) algorithm in tight C code could still be faster than running an O(N) algorithm written in Perl.> P.P.S. Here are my results with the correct code. Grep is > still faster than the other algorithms, so the original > question still stands (how can Perl execute an O(N) plus > an O(N log N) algorithm faster than a single O(N)?)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE:(2) Benchmark.pm's scoping behavior
by Russ (Deacon) on Jun 21, 2000 at 10:51 UTC |