Re: Why is for() less efficient than foreach?

2017-02-10 Thread Dukc via Digitalmars-d-learn
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).

Re: Why is for() less efficient than foreach?

2017-02-10 Thread Bastiaan Veelo via Digitalmars-d-learn
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.

Re: Why is for() less efficient than foreach?

2017-02-10 Thread Bastiaan Veelo via Digitalmars-d-learn
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

Re: Why is for() less efficient than foreach?

2017-02-10 Thread evilrat via Digitalmars-d-learn
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:

Re: Why is for() less efficient than foreach?

2017-02-10 Thread evilrat via Digitalmars-d-learn
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

Re: Why is for() less efficient than foreach?

2017-02-10 Thread Stefan Koch via Digitalmars-d-learn
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 <

Re: Why is for() less efficient than foreach?

2017-02-10 Thread biozic via Digitalmars-d-learn
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.

Why is for() less efficient than foreach?

2017-02-10 Thread Bastiaan Veelo via Digitalmars-d-learn
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;