| Thread overview | ||||||||
|---|---|---|---|---|---|---|---|---|
|
November 23, 2008 foreach | ||||
|---|---|---|---|---|
| ||||
Does foreach use delegates? Isn't that unnecessary overhead? --Sam | ||||
November 23, 2008 Re: foreach | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Sam S E | On Sat, Nov 22, 2008 at 11:40 PM, Sam S E <eisenstat.aa@sympatioco.ca> wrote:
> Does foreach use delegates? Isn't that unnecessary overhead? --Sam
It does use delegates, for iterating over most types. When iterating over arrays, the compiler turns it into a sort of for loop instead.
Is it unnecessary overhead? It's not always as fast as it could be, but unless someone can figure out some other way of implementing it, it's pretty much the best we can get.
How about iterator objects, like in C++ or Java? Are they unnecessary overhead? ;)
| |||
November 23, 2008 Re: foreach | ||||
|---|---|---|---|---|
| ||||
On Sun, Nov 23, 2008 at 3:25 PM, Jarrett Billingsley <jarrett.billingsley@gmail.com> wrote: > On Sat, Nov 22, 2008 at 11:40 PM, Sam S E <eisenstat.aa@sympatioco.ca> wrote: >> Does foreach use delegates? Isn't that unnecessary overhead? --Sam > > It does use delegates, for iterating over most types. When iterating over arrays, the compiler turns it into a sort of for loop instead. > > Is it unnecessary overhead? It's not always as fast as it could be, but unless someone can figure out some other way of implementing it, it's pretty much the best we can get. > > How about iterator objects, like in C++ or Java? Are they unnecessary overhead? ;) How does delegate overhead compare to the stack thread context switch overhead in the new Fibers in druntime? --bb | ||||
November 23, 2008 Re: foreach | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley Wrote:
> When iterating over arrays, the compiler turns it into a sort of for loop instead.<
Sort of is the key word here :-)
It's not as light as a normal for, but in 98% situations is fine.
Bye,
bearophile
| |||
November 23, 2008 Re: foreach | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter wrote:
> On Sun, Nov 23, 2008 at 3:25 PM, Jarrett Billingsley
> <jarrett.billingsley@gmail.com> wrote:
>> On Sat, Nov 22, 2008 at 11:40 PM, Sam S E <eisenstat.aa@sympatioco.ca> wrote:
>>> Does foreach use delegates? Isn't that unnecessary overhead?
>>> --Sam
>> It does use delegates, for iterating over most types. When iterating
>> over arrays, the compiler turns it into a sort of for loop instead.
>>
>> Is it unnecessary overhead? It's not always as fast as it could be,
>> but unless someone can figure out some other way of implementing it,
>> it's pretty much the best we can get.
>>
>> How about iterator objects, like in C++ or Java? Are they unnecessary
>> overhead? ;)
>
> How does delegate overhead compare to the stack thread context switch
> overhead in the new Fibers in druntime?
>
> --bb
~32 instructions per switch and 4KB allocated for fibers, versus about 2 instructions and no allocation for delegates.
| |||
November 23, 2008 Re: foreach | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Christopher Wright | On Sun, Nov 23, 2008 at 11:26 PM, Christopher Wright <dhasenan@gmail.com> wrote:
> Bill Baxter wrote:
>>
>> On Sun, Nov 23, 2008 at 3:25 PM, Jarrett Billingsley <jarrett.billingsley@gmail.com> wrote:
>>>
>>> On Sat, Nov 22, 2008 at 11:40 PM, Sam S E <eisenstat.aa@sympatioco.ca> wrote:
>>>>
>>>> Does foreach use delegates? Isn't that unnecessary overhead? --Sam
>>>
>>> It does use delegates, for iterating over most types. When iterating over arrays, the compiler turns it into a sort of for loop instead.
>>>
>>> Is it unnecessary overhead? It's not always as fast as it could be, but unless someone can figure out some other way of implementing it, it's pretty much the best we can get.
>>>
>>> How about iterator objects, like in C++ or Java? Are they unnecessary overhead? ;)
>>
>> How does delegate overhead compare to the stack thread context switch overhead in the new Fibers in druntime?
>>
>> --bb
>
> ~32 instructions per switch and 4KB allocated for fibers, versus about 2 instructions and no allocation for delegates.
Oh, that's a bit worse than I was thinking. Not a good default iteration strategy then. Bummer.
--bb
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply