Bartosz Milewski seems to like D more than C++ now :)

Paul Jurczak pauljurczak at yahoo.com
Sat Sep 21 21:58:07 PDT 2013


On Friday, 20 September 2013 at 16:28:49 UTC, Joseph Rushton 
Wakeling wrote:
[...]
> The canonical example would be something like,
>
>     foreach (i; iota(10)) { ... }
>
> which in theory shouldn't be any slower than,
>
>     foreach (i; 0 .. 10) { ... }
>
> but in practice is, no matter what the compiler.

Some compilers are unexpectedly smart in this case. I just 
benchmarked somewhat similar code with two version of the same 
function: one using a straight for loop, the other mixture of 
iota and "..". Plain for loop should be the fastest, right? 
Almost right. Here are the functions:

int e28_0(int N = 1002) {
	int diagNumber = 1;					
	int sum        = diagNumber;	

	for (int width = 2; width < N; width += 2)	
		for (int j = 0; j < 4; ++j) {			
			diagNumber += width;				
			sum        += diagNumber;			
		}

	return sum;
}

int e28_1(int N = 1002) {
	int diagNumber = 1;					
	int sum        = diagNumber;	

	foreach (width; iota(2, N, 2))
		foreach (_; 0..4) {			
			diagNumber += width;				
			sum        += diagNumber;			
		}

	return sum;
}

Here are the results:

GDC 4.8.1:     gdc-4.8 -m64 -march=native -fno-bounds-check 
-frename-registers -frelease -O3
669171001  830ns  e28_0
669171001  830ns  e28_1

DMD64 2.063.2: dmd -O -noboundscheck -inline -release
669171001  1115ns  e28_0
669171001  1958ns  e28_1

LDC 0.11.0: ldmd2 -m64 -O -noboundscheck -inline -release
669171001  454ns  e28_0
669171001  395ns  e28_1



More information about the Digitalmars-d mailing list