User defined operators = functions? (+ More)

Started by
5 comments, last by Krylloan 22 years, 2 months ago
When you use a user defined operator, (eg multiplication on a vector structure), does the compiler place a call to the operator ''function'' (Slow)? Does it actually place the operator code in with the rest of the code (so you have one copy of the operator code per use of the operator? - Fast), or does it do something in between, like a Jump? At the moment I use macros for all vertex and matrix structure operations, but they''re a lot uglier and more difficult than operators. Yet speed is very important. (I''m using Visual C++ 6.0 if that makes a difference) Secondly, the register keyword: if you don''t use it, does the compiler decide to place in registers whatever variables it thinks best, or does it not use register variable storage and use stack/ram storage instead?. Thirdly, can you stuff your program up by using the register keyword - eg if you have a register variable and you want a pointer to it. (Pointers to registers don''t exist, right?) Thanks for any assistance Simon Hill
Advertisement
Overloaded operators are functions. However, like all functions, they can be inlined. When they are inlined, they have all the speed advantages of macros.
1) As TerranFury said, operators are functions and they can be inlined.

2) If you don''t use the register keyword, the variables are stored in memory, the RAM stack. Even if you do use the register keyword, it may be ignored if the compiler thinks it will need all available registers for processing.

3) You can''t make a pointer to a register, however this issue should not even arise because if you do decide to use the register keyword, you should only use it for temporary primitives whose lifespan is very short.

~CGameProgrammer( );

~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
In today''s day&age of optimizing compilers, you''re better off not even typing the register keyword. (and the collary is, volatile is suddenly much more important now than it was before).
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
If you''re using Visual C++, it silently ignores the register keyword anyway. There''s no way you could allocate registers as well as it does, and it knows it.

Also don''t assume that inlining is necessarily the fastest alternative; again, let the compiler choose. Just structure your program well and optimize the hot spots later once you''ve profiled the whole app.

--
Eric
-- Eric
I don''t see any reason not to inline... for short functions that are called many times in a small time period, it''s always best to inline, I feel.

~CGameProgrammer( );

~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
Again, not necessarily. If you can keep your code very small you can fit more of it onto the CPU L1 cache; if you can get all your critical code into that cache you''ll be flying (or at least not CPU bound).

You can force a function to be inlined by rewriting it as a macro, but the resulting code bloat may screw up that caching or (conceivably) your paging behavior. Choosing whether to inline is the kind of thing that compilers really excel at, which is why I said to let the compiler choose. Go ahead and mark your frequently used utility functions inline so the compiler can inline if that would be optimal, but don''t force the issue.

--
Eric
-- Eric

This topic is closed to new replies.

Advertisement