Thread overview | ||||||
---|---|---|---|---|---|---|
|
May 31, 2016 Is there any overhead iterating over a pointer using a slice? | ||||
---|---|---|---|---|
| ||||
In relation to this thread: http://forum.dlang.org/thread/ddckhvcxlyuvuiyazpqy@forum.dlang.org Where I asked about slicing a pointer, I have another question: If I have a pointer and iterate over it using a slice, like this: T* foo = &data; foreach (element; foo[0 .. length]) { ... } Is there any overhead compared with pointer arithmetic in a for loop? |
May 31, 2016 Re: Is there any overhead iterating over a pointer using a slice? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | On Tuesday, 31 May 2016 at 18:55:18 UTC, Gary Willoughby wrote:
> Is there any overhead compared with pointer arithmetic in a for loop?
Very very little. The slice will ensure start and stop indexes are in bounds before the loop (and throw an RangeError if it isn't), but inside the loop, it should generate exactly the same code.
|
May 31, 2016 Re: Is there any overhead iterating over a pointer using a slice? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | On Tuesday, 31 May 2016 at 18:55:18 UTC, Gary Willoughby wrote: > > If I have a pointer and iterate over it using a slice, like this: > > T* foo = &data; > > foreach (element; foo[0 .. length]) > { > ... > } > > Is there any overhead compared with pointer arithmetic in a for loop? Use the assembly output of your compiler to check! :-) It's fun to look at. For example, with GDC: http://goo.gl/Ur9Srv No difference. cheers, Johan |
June 01, 2016 Re: Is there any overhead iterating over a pointer using a slice? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johan Engelen | On Tuesday, 31 May 2016 at 20:52:20 UTC, Johan Engelen wrote:
> On Tuesday, 31 May 2016 at 18:55:18 UTC, Gary Willoughby wrote:
>>
>> If I have a pointer and iterate over it using a slice, like this:
>>
>> T* foo = &data;
>>
>> foreach (element; foo[0 .. length])
>> {
>> ...
>> }
>>
>> Is there any overhead compared with pointer arithmetic in a for loop?
>
> Use the assembly output of your compiler to check! :-) It's fun to look at.
> For example, with GDC:
> http://goo.gl/Ur9Srv
>
> No difference.
>
> cheers,
> Johan
That's pretty nice.
|
Copyright © 1999-2021 by the D Language Foundation