Now, the performance numbers; it's pretty easy to test all the variants given so far with Benchmark.pm:my @a = split/:/,$str; @a % 2 && die; # not an even number of items my $r = int(rand(100)); my ($adId, $p); while (($p,$adId) = splice @a,0,2) { if ($r < $p) {last;} $r -= $p; }
I have the benchmark code, but it's basically just a bunch of cuts and pastes of the code that's been posted on this thread, so I'll only post it if asked.Rate origCode duff QM L~R ccn fizbin origCode 4029/s -- -34% -77% -82% -87% -89% duff 6137/s 52% -- -65% -73% -81% -83% QM 17360/s 331% 183% -- -24% -46% -53% L~R 22908/s 469% 273% 32% -- -29% -38% ccn 32106/s 697% 423% 85% 40% -- -12% fizbin 36690/s 811% 498% 111% 60% 14% --
Update: Fixed oops in code, updated performance figures.
-- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/
In reply to Re: $str to %hash to @ary
by fizbin
in thread $str to %hash to @ary
by abaxaba
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |