Darby has asked for the wisdom of the Perl Monks concerning the following question:
I just got the Perl CD Bookshelf version 4,and I am going through the exercises for the "learning Perl Objects, References, and Modules book.
Chapter 7 ex 2 has you use Benchmark to compare the Schwartzian transformation versus a straight sort using -s.
According to the answer, using the Schwartzian transformation should be faster, but I'm getting the opposite results.
The Relevant code is:
I'm running SuSE 9.1 with the perl version that came with it.
uname -a yields:
My results are:use Benchmark qw(timethese); timethese( -2, { Ordinary => q{ my @results = sort { -s $a <=> -s $b } glob "/bin/*"; }, Schwartzian => q{ my @sorted = map $_->[0], sort { $a->[1] <=> $b->[1] } map [$_, -s $_], glob "/bin/*"; }, });
If I do a 5000 iterations instead of 2 seconds, I get:Ordinary: 2 wallclock secs ( 0.61 usr + 1.64 sys = 2.25 CPU) @ 23 +7.78/s (n=535) Schwartzian: 2 wallclock secs ( 1.52 usr + 0.76 sys = 2.28 CPU) @ 2 +02.19/s (n=461)
The only reason I can think of is that I have a hyperthreading processor. Would that make the difference?Benchmark: timing 5000 iterations of Ordinary, Schwartzian... Ordinary: 21 wallclock secs ( 6.46 usr + 14.78 sys = 21.24 CPU) @ 23 +5.40/s (n=5000) Schwartzian: 26 wallclock secs (17.15 usr + 8.45 sys = 25.60 CPU) @ 1 +95.31/s (n=5000)
I'm running SuSE 9.1 with the perl version that came with it.
uname -a yields:
Linux dell 2.6.5-7.104-smp #1 SMP Wed Jul 28 16:42:13 UTC 2004 i686 i6 +86 i386 GNU/Linux
Perl -V yields:
Thanks a lot.
Summary of my perl5 (revision 5.0 version 8 subversion 3) configuratio +n: Platform: osname=linux, osvers=2.6.4, archname=i586-linux-thread-multi uname='linux d209 2.6.4 #1 smp thu mar 11 17:56:49 utc 2004 i686 i +686 i386 gnulinux ' config_args='-ds -e -Dprefix=/usr -Dvendorprefix=/usr -Dinstallusrbinp +erl -Dusethreads -Di_db -Di_dbm -Di_ndbm -Di_gdbm -Duseshrplib=true -Dopti +mize=-O2 -march=i586 -mcpu=i686 -fmessage-length=0 -Wall -Wall -pipe' hint=recommended, useposix=true, d_sigaction=define usethreads=define use5005threads=undef useithreads=define usemulti +plicity=define useperlio=define d_sfio=undef uselargefiles=define usesocks=undef use64bitint=undef use64bitall=undef uselongdouble=undef usemymalloc=n, bincompat5005=undef Compiler: cc='cc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS +-fno-strict-aliasing -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64', optimize='-O2 -march=i586 -mcpu=i686 -fmessage-length=0 -Wall -Wal +l -pipe', cppflags='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -fno-stri +ct-aliasing' ccversion='', gccversion='3.3.3 (SuSE Linux)', gccosandvers='' intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=1 +2 ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', + lseeksize=8 alignbytes=4, prototype=define Linker and Libraries: ld='cc', ldflags ='' libpth=/lib /usr/lib /usr/local/lib libs=-lnsl -ldl -lm -lcrypt -lutil -lpthread -lc perllibs=-lnsl -ldl -lm -lcrypt -lutil -lpthread -lc libc=, so=so, useshrplib=true, libperl=libperl.so gnulibc_version='2.3.3' Dynamic Linking: dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynami +c -Wl,-rpath,/usr/lib/perl5/5.8.3/i586-linux-thread-multi/CORE' cccdlflags='-fPIC', lddlflags='-shared' Characteristics of this binary (from libperl): Compile-time options: MULTIPLICITY USE_ITHREADS USE_LARGE_FILES PERL +_IMPLICIT_CONTEXT Built under linux Compiled at Apr 3 2004 00:52:08 @INC: /usr/lib/perl5/5.8.3/i586-linux-thread-multi /usr/lib/perl5/5.8.3 /usr/lib/perl5/site_perl/5.8.3/i586-linux-thread-multi /usr/lib/perl5/site_perl/5.8.3 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.3/i586-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.3 /usr/lib/perl5/vendor_perl
|
---|
Back to
Seekers of Perl Wisdom