[Issue 6658] New: Slow short array equality
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon Sep 12 16:20:33 PDT 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6658
Summary: Slow short array equality
Product: D
Version: D2
Platform: x86
OS/Version: Windows
Status: NEW
Keywords: performance
Severity: enhancement
Priority: P2
Component: DMD
AssignedTo: nobody at puremagic.com
ReportedBy: bearophile_hugs at eml.cc
--- Comment #0 from bearophile_hugs at eml.cc 2011-09-12 16:20:16 PDT ---
Equality comparisons between very short arrays is quite slow in D/DMD. When the
arrays are both fixed-sized and short, I suggest to inline item comparisons
instead of calling a runtime function, to speed up the code significantly.
A benchmark:
bool eq1(T, size_t N)(const ref T[N] a, const ref T[N] b) pure nothrow {
return a == b;
}
bool eq2(T, size_t N)(const ref T[N] a, const ref T[N] b) pure nothrow {
foreach (size_t i; 0 .. a.length)
if (a[i] != b[i])
return false;
return true;
}
void main() {
import std.stdio, std.random;
enum size_t N = 3;
enum size_t M = 100;
rndGen.seed(1);
int[N][M] data;
foreach (ref d; data)
foreach (ref x; d)
x = uniform(0, 4);
int total;
foreach (i; 0 .. 20_000)
foreach (j; 0 .. M)
foreach (k; 0 .. M)
static if (1)
total += eq1(data[j], data[k]); // 11.5 seconds
else
total += eq2(data[j], data[k]); // 2.45 seconds
writeln(total);
}
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list