On Friday, 10 February 2017 at 13:33:55 UTC, Bastiaan Veelo wrote:
Thanks, I should have spotted that.
Bastiaan.
No, you don't even have to spot things like that. If you assert()
the result that is. (Not a rant, half of us wouldn't probably
have bothered).
On Friday, 10 February 2017 at 12:58:19 UTC, Stefan Koch wrote:
If you want it to modify the array you have to use a ref elem.
If you do you will see that foreach is a little slower.
Thanks, I should have spotted that.
Bastiaan.
On Friday, 10 February 2017 at 12:57:38 UTC, biozic wrote:
On Friday, 10 February 2017 at 12:39:50 UTC, Bastiaan Veelo
wrote:
void foreach_loop()
{
foreach(n, elem; d[])
elem = a[n] * b[n] / c[n];
}
It's fast because the result of the operation (elem) is
On Friday, 10 February 2017 at 13:13:24 UTC, evilrat wrote:
On my machine (AMD FX-8350) actually almost no difference
oops, it skips flags with -run -_-
sorry
dmd loops.d -release
Function 0 took: 16 ╬╝s and 5 hnsecs
Function 1 took: 55 secs, 262 ms, 844 ╬╝s, and 6 hnsecs
Function 2 took:
On Friday, 10 February 2017 at 12:39:50 UTC, Bastiaan Veelo wrote:
Depending on the machine this is run on, for() performs a
factor 3-8 slower than foreach(). Can someone explain this to
me? Or, taking for() as the norm, how can foreach() be so
blazingly fast?
Thanks!
On my machine (AMD
On Friday, 10 February 2017 at 12:39:50 UTC, Bastiaan Veelo wrote:
Benchmarking for() against foreach():
/
enum size_t maxarray = 500_000;
double[maxarray] a, b, c, d;
void main()
{
import std.stdio;
import std.datetime;
import std.random;
for (int n = 0; n <
On Friday, 10 February 2017 at 12:39:50 UTC, Bastiaan Veelo wrote:
void foreach_loop()
{
foreach(n, elem; d[])
elem = a[n] * b[n] / c[n];
}
It's fast because the result of the operation (elem) is discarded
on each iteration, so it is probably optimized away.
Benchmarking for() against foreach():
/
enum size_t maxarray = 500_000;
double[maxarray] a, b, c, d;
void main()
{
import std.stdio;
import std.datetime;
import std.random;
for (int n = 0; n < maxarray; n++)
{
a[n] = uniform01;
b[n] = uniform01;