PC Review


Reply
Thread Tools Rate Thread

Compiler optimization question

 
 
Lloyd Dupont
Guest
Posts: n/a
 
      6th Sep 2005
just curiosity because I guess there isn't much I could do about it....

if I write
byte[] buf = ...;
int N = ...;
for(int i=0; i<N; i++)
do(buf[i]);

will the code do a bound checking on the array on every single access to
buf[i] or will it recognises that N, and buf.Length are constant and
optimize to only one bounds checking?

--
If you're in a war, instead of throwing a hand grenade at the enemy, throw
one of those small pumpkins. Maybe it'll make everyone think how stupid war
is, and while they are thinking, you can throw a real grenade at them.
Jack Handey.


 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      6th Sep 2005
Lloyd Dupont <(E-Mail Removed)> wrote:
> just curiosity because I guess there isn't much I could do about it....
>
> if I write
> byte[] buf = ...;
> int N = ...;
> for(int i=0; i<N; i++)
> do(buf[i]);
>
> will the code do a bound checking on the array on every single access to
> buf[i] or will it recognises that N, and buf.Length are constant and
> optimize to only one bounds checking?


It's less likely to do it with the code above than if you do:

for (int i=0; i < buf.Length; i++)
{
do (buf[i]);
}

However, an even better way would be:

foreach (int x in buf)
{
do (x);
}

Readability is almost always far more important than micro-optimisation
like this anyway.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Lloyd Dupont
Guest
Posts: n/a
 
      7th Sep 2005
Allright.
Anyway I was using N because I didn't want to use buf.Length for the simple
reason I DON'T compare the whole buffer.

It was not micro-optimization, it was plain program logic ;-)


"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Lloyd Dupont <(E-Mail Removed)> wrote:
>> just curiosity because I guess there isn't much I could do about it....
>>
>> if I write
>> byte[] buf = ...;
>> int N = ...;
>> for(int i=0; i<N; i++)
>> do(buf[i]);
>>
>> will the code do a bound checking on the array on every single access to
>> buf[i] or will it recognises that N, and buf.Length are constant and
>> optimize to only one bounds checking?

>
> It's less likely to do it with the code above than if you do:
>
> for (int i=0; i < buf.Length; i++)
> {
> do (buf[i]);
> }
>
> However, an even better way would be:
>
> foreach (int x in buf)
> {
> do (x);
> }
>
> Readability is almost always far more important than micro-optimisation
> like this anyway.
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Negative optimization in C# compiler? Gianluca Microsoft C# .NET 7 24th May 2005 02:01 AM
Compiler/JIT optimization causing problems Prasad Dabak Microsoft C# .NET 1 7th Jan 2005 04:57 PM
Compiler optimization? Tomer Microsoft Dot NET Compact Framework 1 23rd Dec 2004 08:19 PM
JIT compiler global optimization ? =?Utf-8?B?Q2FybA==?= Microsoft Dot NET 3 24th Aug 2004 12:47 PM
Re: Compiler optimization of enum... Cody Manix Microsoft C# .NET 0 15th Jul 2003 03:11 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:04 PM.