Implicit access to rgbsurface without prior declaration

I am wondering if I can access a pixel from a graphics object somepicture = new picture(nx,ny,16)
by using an implicit rgbsurface extension such that

colorvalue = somepicture.rgbsurface.pixel()

rather than the two statement approach shown below

dim surf as rgbsurface = somepicture.rgbsurface
colorvalue = surf.pixel()

My experimentation suggests that both approaches are identical, but I am then wondering why in the language reference the dim surf approach is recommended.

If you’re interested in really finding out, this seems like it would be easy to do some code profiling and/or timing to figure out which way is faster or if the difference is negligible. And there might be a difference on whether you are simply reading the pixels or also writing to them. I seem to remember an old thread that came up with an optimum way to do every pixel in a picture but I don’t have the link handy.

If you are accessing a single pixel, then the 2 are indeed identical. If you want to access more than one pixel, then calling rgbsurface multiple times will incur some amount of overhead. Caching the result in a dimmed variable improves the performance of your code.

Tim and Bob,

Thanks for your comments. I did the profiling suggested by Bob and found that in my application, the two approaches were nearly identical in speed, but I was only grabbing a single pixel at a time to see if I needed to trim the picture. But my main concern was not speed so much as code reliability. I was worried that what appeared to me to be a potentially dangerous shortcut would cause problems. Tim’s response indicates my shortcut is indeed ok, if not potentially damaging to overall code speed.

So the question that remains, Tim, is this. Does not caching the rgbsurface in a dimmed variable not also represent a performance hit? Is it not possible that the use of the extension (without caching) would be faster when only a few pixels are being manipulated? And following Bob’s comment, is there a big difference in reading versus writing a pixel when using the extension?

No
In fact if you have to access many pixels caching the intermediate objects then skips needing to reacquire those references every time and may provide a speed improvement

[quote=328901:@Robert Birge]Is it not possible that the use of the extension (without caching) would be faster when only a few pixels are being manipulated?
[/quote]
more than 1 pixel you’d probably save time caching the reference

There can be
Again the more pixels you read / write the more caching will save you time

Thanks Norman. I will cache from now on whenever more than one pixel is to be accessed.

Note that the compiler creates a temporary variable behind the scenes anyway. The compiler separates the call into discrete events for you. Ie., this code

colorvalue = somepicture.rgbsurface.pixel()

is converted into

dim temp1 as rgbsurface = somepicture.rgbsurface
colorvalue = temp1.pixel()

That’s why I said that for a single pixel, they really are identical. If you do more than one pixel, you’re just throwing away the temp variable that the compiler provides each time. So you might as well provide your own local variable and save some cpu cycles (and memory).