When it comes to speed, pointers are references are essentially equal - a reference, when it gets compiled, gives identical code to the same solution using pointers [1]. The detail differences between pointers and references are mainly in what the compiler allows you to do and what you have to type into your source file to let it know what you want to have happen.
So a reference and pointer (at least when we pass them to functions) will have the same benefits and drawbacks:
1. Passing a pointer/reference passes a pointer-sized element.
2. Dereferencing such a pointer/reference will incur some overhead compared to having the object directly available.
[1] Assuming we're not looking for strange & unusual ways of using either pointers or references, and equal level of understanding from the compiler as to what the code tries to achieve - some compilers occasionally get confused by one or the other concept in some circumstances, but please ignore this for now, I'm just adding this stuff to prevent someone else from contradicting me with regards to details which will just confuse the matter vs. the general rule [Just like when we study physics at a basic level, everything works according to Newton's equations, but once we get deeper into physics, we learn about Einstein's relativity theory, which "break" Newton's rules].
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Well, technically it is myself that calls me a Kernel Hacker, but in the last many years, I have spent most of my time dealing with code that one way or another operates within or in close conjunction with OS kernels, working on Real-time OS's [at a commercial level at least three different ones], Linux and Windows. In Windows I worked for a graphics card producer, writing the Windows Driver for the graphics card [mostly dealing with Low-Level driver stuff, rather than the more interesting 3D side of things], in other work I have been working either for a processor manufacturer or the OS manufacturer.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Sort of correct - but I would call them Kernel API functions, and whilst ReadProcessMemory() in itself doesn't call directly into the kernel [it goes through a API translation layer], it will directly cause kernel code to run and perform the requested operation [if allowed].
Some API calls may of course not enter the kernel at all - they may be part of the user-mode side of the API and fulfill the entire functionality in user-mode.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Speed of your parameter passing will be negligible compared to actually running a database query.
No, I'd just rather have my engineers focussed on optimizing their DB queries, caching of results, threading, etc. than trivial issues like this.
Sure. Particularly "remembering" the result from a previous query rather than redoing it a few lines further on in the code can have dramatic effect on performance, compared to the "a few dozen clock-cycles" which you'd save on passing something with const reference.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Sutter and Alexandrescu's advice to "don't pessimize prematurely" should be applied here: sure, there are larger gains to be had by optimising database queries and choosing appropriate indices, but appropriately choosing to use pass by reference instead of pass by value neither requires considerable thought nor results in code obfuscation, so it might as well be done.No, I'd just rather have my engineers focussed on optimizing their DB queries, caching of results, threading, etc. than trivial issues like this.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Aside from Laserlight (excellent) reply, I would also point out that missing the const reference on a rather long string that may actually be passed along SEVERAL TIMES before it reaches the actual database interface can have a noticeable effect on performance too - sure it's nothing like halving the number of DB queries for a whole application, but copying a kilobyte of query string say a dozen times between the user-interface-code and the actual query will be significantly slower than passing 4 bytes of reference a dozen times. Having the const reference there in the first place will prevent someone else from "messing up" the interface where the string is passed from one object to the next.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I don't disagree with either reply, just hoping that the OP realizes the importance of profiling to find real bottlenecks, and that they're usually tied to the design, algorithms, etc.