Skip to content

Instantly share code, notes, and snippets.

@kfly8 kfly8/bench1.pl
Last active Aug 29, 2015

Embed
What would you like to do?
use strict;
use warnings;
use utf8;
use Benchmark qw/:hireswallclock/;
my $x = 20141024;
my $b = timeit(10000000, sub { $x ** 2 }); # => isa 'Benchmark'
print timestr($b);
# => 0.385485 wallclock secs ( 0.38 usr + 0.00 sys = 0.38 CPU) @ 26315789.47/s (n=10000000)
# 実行するのに、0.385485 秒かかり = レスポンス
# 1CPU時間当たりなら26315789.47だけ処理できる換算 = スループット
# スループットの方がCPU時間になっている事に注意
```
my $elapsed = do {
if ($style eq 'nop') {$cu+$cs}
elsif ($style eq 'noc') {$pu+$ps}
else {$cu+$cs+$pu+$ps}
};
$s .= sprintf(" @ %$f/s (n=$n)",$n/($elapsed)) if $n && $elapsed;
```
use strict;
use warnings;
use utf8;
use Benchmark qw/:hireswallclock/;
my $x = 20141024;
timethis(10000000, sub { $x ** 2 });
#timethis 10000000: 0.347705 wallclock secs ( 0.34 usr + 0.00 sys = 0.34 CPU) @ 29411764.71/s (n=10000000)
use strict;
use warnings;
use utf8;
use Test::More;
use Benchmark qw/:hireswallclock/;
my $x = 20141024;
my $b = timethis(10000000, sub { $x ** 2 }, '$x**2', 'none');
isa $b,'Benchmark';
use strict;
use warnings;
use utf8;
use Benchmark qw/:hireswallclock/;
my $x = 20141024;
my $a = timethis(10000000, sub { $x ** 2 }, '$x**2', 'none');
my $b = timethis(10000000, sub { $x * $x }, '$x*$x', 'none');
# bench $a から bench $bを引いた値
my $d = timediff($a, $b);
print timestr($d);
# => -0.133257 wallclock secs (-0.13 usr + 0.00 sys = -0.13 CPU)
use strict;
use warnings;
use utf8;
use Benchmark qw/:hireswallclock/;
my $x = 20141024;
timethese(10000000, +{
'$x**2' => sub { $x ** 2 },
'$x*$x' => sub { $x * $x },
});
# =>
#Benchmark: timing 10000000 iterations of $x*$x, $x**2...
#$x*$x: 0.578098 wallclock secs ( 0.58 usr + 0.01 sys = 0.59 CPU) @ 16949152.54/s (n=10000000)
#$x**2: 0.448098 wallclock secs ( 0.44 usr + 0.00 sys = 0.44 CPU) @ 22727272.73/s (n=10000000)
use strict;
use warnings;
use utf8;
use Benchmark qw/:hireswallclock cmpthese/;
my $x = 20141024;
my $res = cmpthese(10000000, +{
'$x**2' => sub { $x ** 2 },
'$x*$x' => sub { $x * $x },
}, 'none');
use Data::Dump qw/dump/;
say duump $res;
#=>
#[
# ["", "Rate", "\$x*\$x", "\$x**2"],
# ["\$x*\$x", "20408163/s", "--", "-0%"],
# ["\$x**2", "20408163/s", "0%", "--"],
#] at bench7.pl line 13.
use strict;
use warnings;
use utf8;
use Benchmark qw/:hireswallclock cmpthese/;
use JSON;
use Time::HiRes qw/nanosleep/;
my $res = cmpthese(100000, +{
map {
my $x = $_;
($x => sub { nanosleep($x ** 2) })
} (1..100)
}, 'none');
print encode_json([
map {
(my $y = $_->[1]) =~ s{/s$}{};
[
$_->[0], # key
$y + 0, # Rate
]
} sort { $a->[0]+0 <=> $b->[0]+0 } @$res
]);
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable(
[["x","Rate"],["1",62893],["2",53476],["3",56180],["4",54348],["5",66225],["6",58480],["7",60606],["8",61350],["9",62500],["10",53476],["11",48309],["12",53191],["13",47847],["14",50505],["15",51020],["16",49505],["17",50000],["18",52910],["19",51282],["20",53476],["21",51282],["22",50000],["23",52910],["24",50761],["25",52632],["26",53763],["27",52356],["28",50761],["29",52632],["30",55249],["31",52632],["32",53476],["33",52632],["34",51020],["35",52910],["36",50761],["37",50505],["38",53476],["39",51020],["40",54645],["41",50761],["42",53763],["43",53476],["44",51546],["45",70423],["46",66225],["47",63694],["48",60241],["49",61350],["50",63694],["51",58480],["52",62893],["53",66667],["54",61728],["55",63291],["56",67114],["57",62893],["58",62500],["59",63291],["60",68966],["61",66225],["62",65359],["63",66667],["64",72464],["65",82645],["66",98039],["67",93458],["68",125000],["69",116279],["70",98039],["71",117647],["72",106383],["73",119048],["74",125000],["75",136986],["76",114943],["77",135135],["78",109890],["79",136986],["80",133333],["81",105263],["82",120482],["83",135135],["84",136986],["85",108696],["86",133333],["87",129870],["88",138889],["89",140845],["90",131579],["91",123457],["92",119048],["93",114943],["94",102041],["95",66225],["96",58480],["97",52632],["98",54945],["99",64516],["100",44843]]
);
var options = {
title: 'parabora'
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<pre>nanosleep($x ** 2)</pre>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.