1

I have a Windows 10 computer and Visual Studio 2017 for my C/C++ development.

I am currently reading "Code Optimization: Effective Memory Usage" and the author suggest running the following code to capture accurate run times of various codes.

XOR EAX, EAX    ;CPUID command is called to ensure all preceding commands
CPUID           ;have left the pipeline, hence, not influencing measurement results

RDTSC           ;This returns to the EAX register the lower DWord of current
                ;value of time stamp counter

MOV [clock], EAX  ; the obtained result is save to the clock variable

//...
//profiled code  ; here the profiled code is run
//--- 

XOR EAX, EAX     ; the CPUID command is executed again to ensure all preceding
CPUID            ;instructions have left the pipeline

RDTSC            ; read the new value of the time stamp counter

SUB EAX, [clock] ; calculate the difference to capture actual time to execute code fragment

I can understand what the above assembly code is doing. But where exactly do I type this in in Windows and observe the results? Should I be saving this as a .asm file in Visual studio and then running it within that? Should I create a C file and then include this assembly instructions within it? Or, should I stay away from Visual Studio completely and use a stand alone Assembly Language compiler/debugger?

Tryer
  • 3,580
  • 1
  • 26
  • 49
  • If you're going to put code under test into a separate executable as a microbenchmark, you can do much better than RDTSC. You can use performance counters to count time in core clock cycles, instead of reference cycles, and you can use perf counters to see cache misses, pipeline stalls of various kinds, etc. etc. – Peter Cordes Oct 05 '17 at 03:24
  • 1
    But yes, you put that in a `.asm` file as `main` or `WinMain`, followed by an exit system call, and build it into an executable. (I don' t know the details for Windows.) – Peter Cordes Oct 05 '17 at 03:25
  • @PeterCordes One question I end up having is this. Suppose we are able to get this to run in Visual studio, etc. What if there is some other application, say a video or graphics application that is also running simultaneously. Will not that application "demand" time and resources from memory/CPU? Will not the processor have to multitask between running my code above as well as catering to the video application? In that case, how trustworthy are the above clock times? – Tryer Oct 05 '17 at 03:29
  • 1
    Do multiple runs on a system that's as idle as possible to control for that. Having a multi-core CPU helps avoid interference from other processes, but they will tie up memory bandwidth which matters for some code. rdtsc measures wall-clock time, but it is high-precision and low overhead, so if you really want to time extremely short things instead of just wrapping a repeat loop around something, it's sometimes usable. I always just use a repeat loop and run under `perf stat` on Linux. (See https://stackoverflow.com/questions/44169342/can-x86s-mov-really-be-free for some examples.) – Peter Cordes Oct 05 '17 at 03:34
  • You don't need to use assembly to access "interesting instructions" in Visual Studio. They are built into the compiler as "intrinsics", like [__rdtsc()](https://msdn.microsoft.com/en-us/library/twchhe95.aspx) – Bo Persson Oct 05 '17 at 11:28
  • 1
    @peter processes running on other cores also screw with your results by causing the turbo ratios to jump around due to the max ratio depending on active cores. This is especially bad if you are counting time, not cycles. – BeeOnRope Oct 05 '17 at 18:24

0 Answers0