blöderidiot
Captain
- Registriert
- Juni 2004
- Beiträge
- 3.878
OK! Man müßte auch eine entsprechende Version ohne if machen können, mit bedingter Ganzzahlmultiplikation.nullPtr schrieb:Ich möchte der Vollständigkeit halber noch hinzufügen, daß man auch eine Version bauen kann, die ganz ohne langsame Division oder Speicherzugriff auskommt:
Das sollte meinen Vermutungen nach ...
Können wir ja mal testen:
Code:
use strict;
use Inline 'C';
use Benchmark qw(:all);
my $c = 0;
my $results = timethese(-10, {
lookup => sub { rot_lut(++$c%10+'0') },
modulo => sub { rot_mod(++$c%10+'0') },
ifcond => sub { rot_if (++$c%10+'0') },
mulcond=> sub { rot_mul(++$c%10+'0') },
});
cmpthese( $results );
__END__
__C__
int rot_lut(int ch) {
static int lut[] = {5,6,7,8,9,0,1,2,3,4};
return lut[ch - '0'] + '0';
}
int rot_mod(int ch) {
return (ch + 5 - '0') % 10 + '0';
}
int rot_if(int ch) {
return ch > '4' ? ch - 5 : ch + 5;
}
int rot_mul(int ch) {
return ch + 5*(((ch<='4')<<1)-1);
}
undifcond: 10 wallclock secs (10.55 usr + 0.00 sys = 10.55 CPU) @ 9487330.71/s (n=100091339)
lookup: 11 wallclock secs (10.49 usr + 0.00 sys = 10.49 CPU) @ 9451580.74/s (n=99147082)
modulo: 10 wallclock secs (10.49 usr + 0.00 sys = 10.49 CPU) @ 9363248.14/s (n=98220473)
mulcond: 11 wallclock secs (10.57 usr + 0.00 sys = 10.57 CPU) @ 9560431.03/s (n=101053756)
Rate modulo lookup ifcond mulcond
modulo 9363248/s -- -1% -1% -2%
lookup 9451581/s 1% -- -0% -1%
ifcond 9487331/s 1% 0% -- -1%
mulcond 9560431/s 2% 1% 1% --
Aber auf einem anderen Rechner (hier i7-3770K) und einem anderen C-Compiler (hier Linux gcc 4.8) kann es schon wieder anders aussehen.Benchmark: running ifcond, lookup, modulo, mulcond for at least 10 CPU seconds...
ifcond: 11 wallclock secs (10.52 usr + 0.00 sys = 10.52 CPU) @ 9514385.84/s (n=100091339)
lookup: 10 wallclock secs (10.53 usr + 0.00 sys = 10.53 CPU) @ 9505350.33/s (n=100091339)
modulo: 11 wallclock secs (10.50 usr + 0.00 sys = 10.50 CPU) @ 9354330.76/s (n=98220473)
mulcond: 11 wallclock secs (10.57 usr + 0.00 sys = 10.57 CPU) @ 9560431.03/s (n=101053756)
Rate modulo lookup ifcond mulcond
modulo 9354331/s -- -2% -2% -2%
lookup 9505350/s 2% -- -0% -1%
ifcond 9514386/s 2% 0% -- -0%
mulcond 9560431/s 2% 1% 0% --
Zuletzt bearbeitet: