3

Is it better to assign a object property to a variable first instead of using it directly in a loop?

Say I have a bitmap Bitmap img = new Bitmap("image.jpg") and I needed to loop through all the pixels to do some processing. For a 1080p image that's about 2 million pixels. Does it matter if I use data.Stride or assign them to a variable first int dataStride = data.Stride? I need to access it each time to calculate the offset but the dataStride is a constant of the image.

data = editImage.LockBits(new Rectangle(0, 0, editWidth, editHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
byte* dataPtr = (byte*)data.Scan0;
dataStride = data.Stride;

I assign them to a variable first since I am thinking that it has to access the object (each time) first and then access the integer from the object (each time) which is slower. And since it is a large loop... it adds up. So assigning the property to a variable first will be faster since it can access the int value directly. Is this correct?

1
  • 1
    Write it both ways and measure performance over multiple runs in release mode, either with System.Diagnostics.StopWatch or a Profiler. There are a lot of corner cases where one or the other may be faster, and unless someone actually measured it against a byte pointer, general purpose information isn't going to be that helpful. Commented May 2, 2012 at 1:01

1 Answer 1

2

Yes. No matter how simple the property, accessing it still has the overhead of a function call. A variable is faster, especially if you are doing something 2 million times.

Sign up to request clarification or add additional context in comments.

8 Comments

I agree with @lukas, compiler and CLR are by far optimized to do the trick (even if you can also help a little by using a local @Ryder).
Yes and no. When looping over an array, the JITter can and usually disable the bounds checking - for(i = 0; i < arr.length; i++) is faster than x = arr.Length; for(i = 0; i < x; i++). However, for properties, this may or may not be the case. If it's simple a field-getter, it may be inlined, but if it's a real method (and I know GDI has some of them), then it can get real expensive. For example, Image.Width is a call to GDI+, so for(int i = 0; i<image.Width;i++) is ridiculously expensive.
@MichaelStum I did for(int i = 0; i<image.Width;i++) before and then changed it to int width = image.Width; for(int i = 0; i<width;i++) and noticed an improvement in speed which is why I thought assigning them to variables first was faster. I ended up doing that for most of my loops... but wasn't sure if that was the case in general.
In general though, you don't know if the getter if the property is doing more processing. In this case you can find out by checking the code behind Bitmap, but what if someone wrote: public string MyProperty { get { Thread.Sleep(100); return "slow?"; } } Yes that person should be shot, but I have seen apps where property getters read from settings files off a disk, or find a field in XML... IMO copying what you need to a local variable is safer when dealing with outside / 3rd party code.
See this article by Jon Skeet for some interesting bits: msmvps.com/blogs/jon_skeet/archive/2009/01/29/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.