Jump to content
  • Advertisement
Sign in to follow this  
kSquared

Is casting O(1)?

This topic is 5057 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

The languages in question here are C++ and C#. Is casting an object to a new type a constant-time operation that's independent of the object's size in memory (that is, it's merely a change in how its bits are interpreted)? Or do larger objects take longer to cast?

Share this post


Link to post
Share on other sites
Advertisement
It depends on the nature of a cast. Checked downcasting can entail a pointer-comparison or worse, but thats all I know for sure. Unchecked casts won't even show up in the generated code.

Share this post


Link to post
Share on other sites
Type determines representation and interpretation of data in memory. Thus casting between types simply changes the interpretation of that data. Changing interpretation in general isn't physical operation, thus it shouldn't take time (except is special cases like inheritance where the compiler might do some internal bookkeeping, or during a few other special casts). Hopefully no compiler-vendor is silly enough to implement a cast that isn't constant-time (if time at all).

Share this post


Link to post
Share on other sites
In C++ it depends on the kind of cast (static_cast, reinterpret_cast, dynamic_cast) as well as the types being cast.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zipster
Hopefully no compiler-vendor is silly enough to implement a cast that isn't constant-time (if time at all).

There are plenty of casts that a standards compliant compiler will be unable to do in constant time. For example: a static_cast of a const char * to a std::string actually creates a temporary std::string object constructed from the const char *. The is decidedly a non-constant time operation.

Share this post


Link to post
Share on other sites
C++ : reinterpret_cast doesn't take any time. Other cast on user-defined types will have time complexity of apporiate constructor, that may have any time complexity you will make. (for example, you can convert from unsorted list to sorted list, using quicksort with O(n*log(n))) time, or you can use bubblesort with O(n2) time...)
Cast from parent to child : dynamic cast will do some checking, don't sure with what time complexity, might depends to multiple/single inheritance , etc. Cast from pointer to child to pointer to parent - i guess, with single inheritance, nothing in resulting code.

Share this post


Link to post
Share on other sites
Slightly OffTopic but if you're using casts in your main program code (i.e. away from loading files, or sending network data), then there is quite possibly a problem with your design.

Share this post


Link to post
Share on other sites
Hmm, I don't think I would say that. For instance, you might want to perform calculations using doubles before casting the final result to a float, when precision is important.

Share this post


Link to post
Share on other sites
I was talking about about your traditional casts with built-in types where just the interpretation changes. Obviously casts which involve custom types (such as std::string) won't be constant time because the new object has to be built rather than just interpreted in a new way.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!