of clocks pulses?
This is a weird statement to make. What do you mean speed of a function? Are you referring to the cycles taken by the call instruction?
I have two implementation that do the same thing How can I measure the speed of them to know which one is faster? Is there any unit or something to do that?
The one with better algorithmic complexity will be faster when the input size increases. For algorithms with the same complexity you can use a benchmarking library to determine which is faster. This will be a small constant factor which might vary from compiler to compiler and architecture to architecture. Generally these sort of optimizations are considered to be premature and tend to be done only when you need to squeeze out every ounce of performance and is typically done in OS code and Game engines and some math libraries. For the rest of the code out there, you generally don't need to do this.
+1 to Madhu and Manav What you are referring to is benchmarking. There are a lot of caveats in benchmarking as a lot of stuff can affect the results (different input data, processor architecture, data layout, other processes running, etc.). You can find some mentioned here: https://m.youtube.com/watch?v=r-TLSBdHe1A If your function is disconnected from other code, then as the first approximation I would suggest to use quickbench https://quick-bench.com/ (Note that there are caveats with it as well, you need to carefully choose what you don't want to optimize to measure the correct thing). If your function is part of the system and you want to measure things together, it can be more tricky depending on how often it is called and how much time it takes.
Обсуждают сегодня