digitalmars.D - foreach is slower than for?
- Stanislav Blinov <stanislav.blinov gmail.com> Sep 08 2010
- =?iso-8859-2?B?VG9tZWsgU293afFza2k=?= <just ask.me> Sep 08 2010
- Justin Johansson <no spam.com> Sep 09 2010
- Jesse PHillips <jessekphillips+D gmail.com> Sep 09 2010
- Stanislav Blinov <stanislav.blinov gmail.com> Sep 09 2010
- Jonathan M Davis <jmdavisprog gmail.com> Sep 09 2010
- Andrej Mitrovic <andrej.mitrovich gmail.com> Sep 09 2010
- Stanislav Blinov <stanislav.blinov gmail.com> Sep 09 2010
I was wondering about it for some time:
void foreach_loop(int arr[100])
{
foreach(i,a; arr)
{
auto e = a;
}
}
void for_loop(int arr[100])
{
for (int i = 0; i < 100; ++i)
{
auto e = arr[i];
}
}
compiled with -O -release yields this:
.text.void main.foreach_loop(int[100]) segment
assume CS:.text.void main.foreach_loop(int[100])
void main.foreach_loop(int[100]):
push EBP
mov EBP,ESP
sub ESP,8
mov EAX,064h
lea ECX,8[EBP]
xor EDX,EDX
mov -8[EBP],EAX
mov -4[EBP],ECX
cmp -8[EBP],EDX
je L23
L1B: add EDX,1
cmp EDX,-8[EBP]
jb L1B
L23: mov ESP,EBP
pop EBP
ret 0190h
nop
nop
nop
.text.void main.foreach_loop(int[100]) ends
.text.void main.for_loop(int[100]) segment
assume CS:.text.void main.for_loop(int[100])
void main.for_loop(int[100]):
push EBP
mov EBP,ESP
xor EAX,EAX
L5: inc EAX
cmp EAX,064h
jb L5
pop EBP
ret 0190h
nop
.text.void main.for_loop(int[100]) ends
Without -O -release the difference is even greater
Is this normal? I mean, shouldn't the compiler optimize foreach a bit
more? Or is it that I simply misunderstand generated code?
Sep 08 2010
Dnia 08-09-2010 o 23:12:55 Stanislav Blinov <stanislav.blinov gmail.com>= = napisa=B3(a):I was wondering about it for some time: void foreach_loop(int arr[100]) { foreach(i,a; arr) { auto e =3D a; } } void for_loop(int arr[100]) { for (int i =3D 0; i < 100; ++i) { auto e =3D arr[i]; } } compiled with -O -release yields this: .text.void main.foreach_loop(int[100]) segment assume CS:.text.void main.foreach_loop(int[100]) void main.foreach_loop(int[100]): push EBP mov EBP,ESP sub ESP,8 mov EAX,064h lea ECX,8[EBP] xor EDX,EDX mov -8[EBP],EAX mov -4[EBP],ECX cmp -8[EBP],EDX je L23 L1B: add EDX,1 cmp EDX,-8[EBP] jb L1B L23: mov ESP,EBP pop EBP ret 0190h nop nop nop .text.void main.foreach_loop(int[100]) ends .text.void main.for_loop(int[100]) segment assume CS:.text.void main.for_loop(int[100]) void main.for_loop(int[100]): push EBP mov EBP,ESP xor EAX,EAX L5: inc EAX cmp EAX,064h jb L5 pop EBP ret 0190h nop .text.void main.for_loop(int[100]) ends Without -O -release the difference is even greater Is this normal? I mean, shouldn't the compiler optimize foreach a bit =
more? Or is it that I simply misunderstand generated code?
Yes, it should; that was the point of having foreach in the language: http://www.d-programming-language.org/faq.html#foreach I think it's just that there's always plenty of more urgent things with = = DMD. Tomek
Sep 08 2010
On 9/09/2010 6:56 AM, Tomek Sowiński wrote:Without -O -release the difference is even greater Is this normal? I mean, shouldn't the compiler optimize foreach a bit more? Or is it that I simply misunderstand generated code?
Yes, it should; that was the point of having foreach in the language: http://www.d-programming-language.org/faq.html#foreach I think it's just that there's always plenty of more urgent things with DMD. Tomek
Is there a roadmap for what things are more urgent? The OP has a valid point in asking what is the point of the language. A decent roadmap would help newcomers and oldcomers alike to stay with the Holy Grail of D. Justin
Sep 09 2010
Justin Johansson Wrote:Is there a roadmap for what things are more urgent? The OP has a valid point in asking what is the point of the language. A decent roadmap would help newcomers and oldcomers alike to stay with the Holy Grail of D. Justin
There is a somewhat official road map[1]. It isn't really set up by importance. Don has been doing a good job of updating it. I don't think the list is used by Walter as a checklist of things to do. It could definitely use a face lift on how the items should be organized. Currently compiling 64 bit binaries is priority. Which I think is scheduled for next release 2.049, currently compiles Phobos. 1. http://prowiki.org/wiki4d/wiki.cgi?LanguageDevel
Sep 09 2010
Justin Johansson wrote:On 9/09/2010 6:56 AM, Tomek Sowiński wrote:Without -O -release the difference is even greater Is this normal? I mean, shouldn't the compiler optimize foreach a bit more? Or is it that I simply misunderstand generated code?
Yes, it should; that was the point of having foreach in the language: http://www.d-programming-language.org/faq.html#foreach I think it's just that there's always plenty of more urgent things with DMD. Tomek
Is there a roadmap for what things are more urgent? The OP has a valid point in asking what is the point of the language. A decent roadmap would help newcomers and oldcomers alike to stay with the Holy Grail of D. Justin
Well, I actually didn't ask 'what's the point of foreach is if it's slower' :) I just wanted to find out if it's really as far as it can get, or is it simply not polished yet. Nevertheless, I agree that seeing some kind of roadmap would be nice, especially now when D2 is shaped to certain extent and someone would consider starting mean projects with it.
Sep 09 2010
On Thursday, September 09, 2010 13:31:13 Stanislav Blinov wrote:Justin Johansson wrote:On 9/09/2010 6:56 AM, Tomek Sowi=F1ski wrote:Without -O -release the difference is even greater =20 Is this normal? I mean, shouldn't the compiler optimize foreach a bit more? Or is it that I simply misunderstand generated code?
Yes, it should; that was the point of having foreach in the language: http://www.d-programming-language.org/faq.html#foreach =20 I think it's just that there's always plenty of more urgent things with DMD. =20 =20 Tomek
Is there a roadmap for what things are more urgent? The OP has a valid point in asking what is the point of the language. A decent roadmap would help newcomers and oldcomers alike to stay with the Holy Grail of D. =20 Justin
Well, I actually didn't ask 'what's the point of foreach is if it's slower' :) I just wanted to find out if it's really as far as it can get, or is it simply not polished yet. =20 Nevertheless, I agree that seeing some kind of roadmap would be nice, especially now when D2 is shaped to certain extent and someone would consider starting mean projects with it.
I don't know about the case with arrays, but if foreach is using opApply(),= it=20 uses a delegate, which is definitely going to be less efficient. Theoretica= lly,=20 opApply and its delegate could be inlined to replace the foreach and defini= tely=20 improve that efficiency (especially because that particular delegate will n= ever be=20 called anywhere else), but that's not currently done. So, there are definit= ely=20 things that can (and likely eventually will) be done to improve the efficie= ncy of=20 foreach as well as other basic stuff, but as said in another post, other st= uff has=20 higher priority. =2D Jonathan M Davis
Sep 09 2010
Are the ones listed under "Known D2" official? There's "Remove C-style declarations." Is this really scheduled for removal= ? On Thu, Sep 9, 2010 at 8:33 PM, Jesse PHillips <jessekphillips+D gmail.com> wrote:Justin Johansson Wrote:Is there a roadmap for what things are more urgent? =A0The OP has a valid point in asking what is the point of the language. =A0A decent roadmap would help newcomers and oldcomers alike to stay with the Holy Grail of D. Justin
There is a somewhat official road map[1]. It isn't really set up by impor=
is used by Walter as a checklist of things to do. It could definitely use = a face lift on how the items should be organized.Currently compiling 64 bit binaries is priority. Which I think is schedul=
1. http://prowiki.org/wiki4d/wiki.cgi?LanguageDevel
Sep 09 2010
Tomek Sowiński wrote:Dnia 08-09-2010 o 23:12:55 Stanislav Blinov <stanislav.blinov gmail.com> napisał(a):I was wondering about it for some time: void foreach_loop(int arr[100]) { foreach(i,a; arr) { auto e = a; } } void for_loop(int arr[100]) { for (int i = 0; i < 100; ++i) { auto e = arr[i]; } } compiled with -O -release yields this: .text.void main.foreach_loop(int[100]) segment assume CS:.text.void main.foreach_loop(int[100]) void main.foreach_loop(int[100]): push EBP mov EBP,ESP sub ESP,8 mov EAX,064h lea ECX,8[EBP] xor EDX,EDX mov -8[EBP],EAX mov -4[EBP],ECX cmp -8[EBP],EDX je L23 L1B: add EDX,1 cmp EDX,-8[EBP] jb L1B L23: mov ESP,EBP pop EBP ret 0190h nop nop nop .text.void main.foreach_loop(int[100]) ends .text.void main.for_loop(int[100]) segment assume CS:.text.void main.for_loop(int[100]) void main.for_loop(int[100]): push EBP mov EBP,ESP xor EAX,EAX L5: inc EAX cmp EAX,064h jb L5 pop EBP ret 0190h nop .text.void main.for_loop(int[100]) ends Without -O -release the difference is even greater Is this normal? I mean, shouldn't the compiler optimize foreach a bit more? Or is it that I simply misunderstand generated code?
Yes, it should; that was the point of having foreach in the language: http://www.d-programming-language.org/faq.html#foreach I think it's just that there's always plenty of more urgent things with DMD. Tomek
Thanks, I hoped I'd see this kind of answer. :)
Sep 09 2010









Jesse PHillips <jessekphillips+D gmail.com> 