Sign in to follow this  

[.net] operators hughe performance impact!

This topic is 4131 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Wow, I just played around some with c# and created a custom container implemented with as an array. And since I'm a big fan of old c++, I made my collection ala std::iterator style. After some performance testing, I found that iterators using overloaded operators iterating over my collection, was approximately 4 times slower than exactly the same operation using normal functions instead of the overloaded operators: from: iter++; to: iter.next(); The code looks like the following:

for(; iter != endIter; ++iter)
{
  //do something simple
}


// VS


for(; iter.compareTo(endIter) != 0; iter.next())
{
  //do something simple
}



To me it's strange that the difference is that big? Why not just make the compiler rewrite the overloaded operators to equivalent functions instead? Maybe I'm totally off here?

Share this post


Link to post
Share on other sites
Hum, I don't think you got it right, I mean't that the second version with the iter.compareTo() and iter.next() was a lot faster than the version with the overloaded operators, even though the operators and the functions do exactly the same thing.

Oh, btw, maybe It's not so obvious that "iter" and "endIter" is a custom class with overloaded operators for != and ++.

Share this post


Link to post
Share on other sites
Oh dang, I'm terribly sorry everyone!

Stupid me measured performance with debug settings, in release settings both versions ran with the same speed.

Really sorry to have caused any confusion :(

Share this post


Link to post
Share on other sites
Also, you're carrying over a horrible feature from the STL: The need for custom iterators.

If you implement the IEnumerable interface on your collection, you can use the foreach() statement, which is so much more readable and easier to use.

I prefer

foreach (Object o in MyCustomCollection)
{
}


over
for (; iter != myCustomCollection.End; ++iter)

anyday [grin]

Toolmaker

Share this post


Link to post
Share on other sites
Quote:
Original post by Toolmaker
Also, you're carrying over a horrible feature from the STL: The need for custom iterators.

If you implement the IEnumerable interface on your collection, you can use the foreach() statement, which is so much more readable and easier to use.

I prefer

foreach (Object o in MyCustomCollection)
{
}


over
for (; iter != myCustomCollection.End; ++iter)

anyday [grin]

Toolmaker

That works great if you're trying to print the contents of your collection. It works considerably less well if you're trying to modify them.

In my own container implementation, I provide both, plus a wrapper class that converts an iterator range into an enumeration. The wrapper class's syntax is still rather ugly, though, since the compiler is unable to figure out the types on its own.

*edit: Insert disclaimer about not reinventing the wheel here.

CM

Share this post


Link to post
Share on other sites

This topic is 4131 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this