Recent processors have internal counters which are incremented every tick of the microprocessor.
By reading these counters, you are able to have the most accurate timer. This means that if the uP switched the context between the two moments, you measure the execution time of other processes also. You may call the function at the beginning and the end of a piece of code and take the difference to see how much time it took to execute. Reads the TimeStampCounter on the Pentium and returns a 64 bit result.