As mentioned in my previous blog post, I hope to examine a more interesting comparison for the different types of methods. Note that this comparison completely ignores the different use-cases for each type, it is therefor only relevant for ‘pure functions’, as in that case it doesn’t matter whether they belong to a class or whether they are static or not.
In comparison to Java, C++ does not force you to make all methods part of a class. Instead you can also have regular methods, which are often used for helper methods that only rely on their input parameters to generate an output. It is obvious that these methods don’t have to be part of a class as they do not require any state, they are what we call pure methods.
But you can also have regular methods and static methods outside of a class. Although this seems odd since isn’t static only saying that it does not require a class to be called, but since the method isn’t part of a class in the first place, what is the point of doing this?
The concept of a static variable exists in every language, and it is a feature that enables multiple objects to share a common variable. It enables communication between these multiple objects and can be used to avoid creating copies of certain data if this data is constant. There is however a big difference between Java and C++, whereas in Java you can only have static members, in C++ you can have static on all levels going from global variables all the way down to (function) local ones.
In this blog post I will briefly discuss the sense and nonsense of static variables and how they perform. Based on that I will give my opinion of when and how they should be used.
In the previous post we established there was little to be gained to manually mark a function as inline, however all of this was done with functions that were declared in a header file as this is a requirement for inlining. In this blog post we will see how an inlined function (declared in the header) compares to a function declared in a source file.
C++ has the option to mark a function for ‘inlining’, this means that the function is ‘copied’ to where it is called and thus removing the overhead of calling the function. I believe to have read somewhere that the compiler can be smart enough to perform this optimization for you and that you shouldn’t care about explicitly marking such a method. It is time to put this hypothesis to the test.
We all write data to the console from time to time, and in C++ you use the ‘std::cout’ stream for this. To keep your information near, you will want to have new lines to separate the different lines, but then the question comes up, what is the best way to write a new line? As far as I know there are two ways to do so, either by printing ‘\n’ (on a Unix system) or by using the ‘std::endl’.
As you may now, the only real difference is that ‘std::endl’ does a flush after writing the new line character, which does cause some overhead. So the question really is, what is the impact of this overhead?
When reading data from a file multiple times, there are basically two main ways to do so. You can either always reopen the file or you can keep the file open and jump to a specific location. With the option to jump to a specific location inside the file, I was wondering exactly how much faster it is to use this function over re-opening the file every time.
Initially my question was more why would anyone re-open a file when C++ offers a way to reset the file pointer to the beginning of the file? In my mind there is no way that the latter could be any faster, and since both have the same effect why ever bother? But of course I had to put my idea to the test.