Welcome to Web-News
A Web-based News Reader
Subject X[]==Y[] is 7X slower than it should be -- why?
From Don <nospam@nospam.com.au>
Date Fri, 20 Jun 2008 17:02:25 +0200
Newsgroups digitalmars.D

I've been doing some timing tests, and found that the built-in array
comparison is amazingly slow.

Given:
uint [2000] X, Y;

bool b = (X[] == Y[]);

on my Pentium M, this takes ~30000 cycles in the case where both arrays
are equal.
It's not hard to write an optimal asm routine; this takes 4000 cycles
(7.5 times faster).
But, it's also possible to perform the comparison in simple D code,
something like:

int compareArrays(uint [] x, uint y[])
{
     for (int i=0; i<x.length; ++i) {
         if (x[i] != y[i]) return x[i]-y[i];
     }
     return 0;
}

and this takes 6000 cycles when compiled with -release -O, 5X better
than the built-in version.

So, why is the built-in compare so slow? Using the CPU performance counters:
X[]==Y[] --> 10000 branches, 35000 memory loads, 30000 cycles.
for loop --> 4000 branches, 6000 memory loads, 6000 cycles.
asm      --> 4000 branches, 4000 memory loads, 4000 cycles.

What's it doing? Is it doing a byte-by-byte comparison? (it shouldn't do
that even for strings). Is it doing bounds checking for every array
element?? (even with -release -O)

There's significant optimisation potential here.

Recent messages in this thread
 
-# X[]==Y[] is 7X slower than it should be -- why? (Current message) Don 20-Jun-2008 11:02 am
.|# Re: X[]==Y[] is 7X slower than it should be -- why? janderson 20-Jun-2008 11:05 am
.|# Re: X[]==Y[] is 7X slower than it should be -- why? Unknown W. Brackets 20-Jun-2008 12:27 pm
.-# Re: X[]==Y[] is 7X slower than it should be -- why? Jarrett Billingsley 20-Jun-2008 01:40 pm
.|-# Re: X[]==Y[] is 7X slower than it should be -- why? Sean Kelly 20-Jun-2008 01:57 pm
.||-# Re: X[]==Y[] is 7X slower than it should be -- why? Sean Kelly 20-Jun-2008 02:47 pm
.||.-# Re: X[]==Y[] is 7X slower than it should be -- why? Jarrett Billingsley 20-Jun-2008 02:52 pm
.||..-# Re: X[]==Y[] is 7X slower than it should be -- why? Sean Kelly 20-Jun-2008 07:13 pm
.||...-# Re: X[]==Y[] is 7X slower than it should be -- why? Tomas Lindquist O... 21-Jun-2008 12:58 pm
.||....-# Re: X[]==Y[] is 7X slower than it should be -- why? Sean Kelly 22-Jun-2008 11:21 am
.||.....-# Re: X[]==Y[] is 7X slower than it should be -- why? Sean Kelly 22-Jun-2008 12:34 pm
.||......-# Re: X[]==Y[] is 7X slower than it should be -- why? Tomas Lindquist O... 22-Jun-2008 12:53 pm
.||.......\# Re: X[]==Y[] is 7X slower than it should be -- why? Sean Kelly 22-Jun-2008 03:09 pm
.|\# Re: X[]==Y[] is 7X slower than it should be -- why? Unknown W. Brackets 20-Jun-2008 03:56 pm
.-# Re: X[]==Y[] is 7X slower than it should be -- why? Sean Kelly 20-Jun-2008 01:53 pm
..-# Re: X[]==Y[] is 7X slower than it should be -- why? downs 20-Jun-2008 06:15 pm
...-# Re: X[]==Y[] is 7X slower than it should be -- why? torhu 21-Jun-2008 09:46 pm
....-# Re: X[]==Y[] is 7X slower than it should be -- why? Jarrett Billingsley 21-Jun-2008 11:33 pm
.....\# Re: X[]==Y[] is 7X slower than it should be -- why? torhu 22-Jun-2008 03:04 pm