[Tokyo.pm] Re: わかりやすいソート (Re: [Tokyo.pm] Re: [Tokyo.pm] JUS感想)

maeda @ src.ricoh.co.jp maeda @ src.ricoh.co.jp
1999年 10月 6日 (水) 23:57:36 CDT


$BA0ED(B@$B%j%3!<$G$9!#(B

| for_loop_once: 48 wallclock secs (48.64 usr +  0.00 sys = 48.64 CPU)
| for_loop_twice: 52 wallclock secs (51.20 usr +  0.00 sys = 51.20 CPU)
| for_sm_once: 47 wallclock secs (47.43 usr +  0.00 sys = 47.43 CPU)
| for_sm_twice: 49 wallclock secs (49.28 usr +  0.00 sys = 49.28 CPU)
| hash: 48 wallclock secs (48.05 usr +  0.00 sys = 48.05 CPU)
| map_once: 182 wallclock secs (181.64 usr +  0.00 sys = 181.64 CPU)
| map_twice: 66 wallclock secs (65.96 usr +  0.00 sys = 65.96 CPU)

$B$s!A!"(B

|          map_once => sub {
|               my (@keys1, @ keys2);
|               map {push(@keys1, getkey1($_)),
|                    push(@keys2,getkey2($_))
|               } @unsorted;
|          },

map$B$d(Bgrep$B$r(Bvoid$B%3%s%F%-%9%H$G8F$s$G$O%@%a$G$9!#(Bpush$B$NJV$jCM(B($BMWAG(B
$B?t(B)$B$r%j%9%H$K=8$a$F$+$i<N$F$F$$$^$9!#(B

|          hash => sub { # Cute and fast.
|               my %hash;
|               my (@keys1, @ keys2);
|               foreach(@unsorted) {
|                    $hash[getkey1($_)]=getkey2($_);
|               };
|
|# Try "$hash[getkey1($_)]=getkey2($_) for @unsorted" for more fun
|
|               @keys1=keys %hash; @keys2=values %hash;
|	   },

hash$B$N(Bkeys$B$O=g=x$rJ]B8$7$J$$$7!"(B @ unsorted$B$KF1$8MWAG$,J#?t$"$k$H(B
$B:$$k$N$G!"%=!<%H%-!<$r:n$k$K$OITE, @ Z$G$7$g$&!#(B

$B0J2<$N$h$&$K$d$k$H!"$[$H$s$I:9$,$J$$$3$H$,$o$+$j$^$9!#(B
$B$3$l$G$b(Bmap_once$B$G$O%`%@$J%j%9%H:n$C$F$kJ,ITMx$G$9$M!#(B

For 5000 elements...
Benchmark: timing 200 iterations of for_loop_once, for_loop_twice, for_sm_once, for_sm_twice, map_once, map_twice...
for_loop_once: 50 wallclock secs (50.02 usr +  0.02 sys = 50.04 CPU)
for_loop_twice: 51 wallclock secs (50.43 usr +  0.03 sys = 50.46 CPU)
for_sm_once: 48 wallclock secs (48.37 usr +  0.02 sys = 48.39 CPU)
for_sm_twice: 49 wallclock secs (48.34 usr +  0.02 sys = 48.36 CPU)
  map_once: 51 wallclock secs (50.90 usr +  0.03 sys = 50.93 CPU)
 map_twice: 46 wallclock secs (46.12 usr +  0.07 sys = 46.19 CPU)

use Benchmark;

open(TEST, "/usr/games/lib/fortunes/fortunes"); { undef $/; $_=<TEST>; } close TEST;
@unsorted=(split)[1..5000]; # Just some random words.

print "For ",scalar @unsorted," elements...\n";
timethese(200,
	  {
	      map_twice => sub {
		  my (@keys1, @ keys2); # Make sure we've a clean slate.
		  @keys1 = map { getkey1($_) } @unsorted;
		  @keys2 = map { getkey2($_) } @unsorted;
		  1;		# avoid returning @keys2
	      },
	      map_once => sub {
		  my (@keys1, @ keys2);
		  map {push(@keys1, getkey1($_)),
		       push(@keys2,getkey2($_));
		       ()	# just a trick
		       } @unsorted;
		  1;
	      },
	      for_sm_twice => sub { # sm == statement modifier
		  my (@keys1, @ keys2);
		  push @keys1, getkey1($_) for @unsorted;
		  push @keys2, getkey2($_) for @unsorted;
		  1;
	      },
	      for_loop_twice => sub {
		  my (@keys1, @ keys2);
		  for (@unsorted) { push @keys1, getkey1($_) };
		  for (@unsorted) { push @keys2, getkey2($_) };
		  1;
	      },
	      for_loop_once => sub {
		  my (@keys1, @ keys2);
		  foreach(@unsorted) {
		      push @keys1, getkey1($_);
		      push @keys2, getkey2($_);
		  }
		  1;
	      },
	      for_sm_once => sub {
		  my (@keys1, @ keys2);
		  (push(@keys1,getkey1($_)),
		   push(@keys2,getkey2($_))) for @unsorted;
		  1;
	      },
	  }
);

# Just some dummy functions
sub getkey1 { return scalar(reverse shift()) }
sub getkey2 { return shift()."foo" }

-------------------------------  Avatar          Md+   d/=$B""(B=\.   Md+
 $BA0ED(B $B70(B maeda @ src.ricoh.co.jp   75t 170km/h   AFC50  O \*oo*/ "  LG+ LG+
 ($B3t(B)$B%j%3!<(B $B%=%U%H%&%'%"8&5f=j(B   HeatSink 15   LRM10   .=X$B!1(BX=.   LRM10
-------------------------------  Armor 2164      Md+  _|_    _|_  Md+



Tokyo-pm メーリングリストの案内