Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / Visual C++ 9.0
Alternative
Tip/Trick

Debugging technique

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
31 Jan 2011CPOL 8.8K   3   3
Here is a fun program that not only shows how to get precise timings by using the time stamp counter, it also shows how to call main recursively. This is a C program. C++ will not allow calls to main.#include "stdio.h"#include "Windows.h" // Required for Sleep#define CPUID __asm __emit...
Here is a fun program that not only shows how to get precise timings by using the time stamp counter, it also shows how to call main recursively. This is a C program. C++ will not allow calls to main.
C
#include "stdio.h"
#include "Windows.h"	// Required for Sleep

#define CPUID __asm __emit 0fh __asm __emit 0a2h
#define RDTSC __asm __emit 0fh __asm __emit 031h //Not needed in latest Microsoft Visual Studio

unsigned __int64 getCycles(void);
unsigned __int64 TNEW, TOLD;

// The following avoids the overhead of saving and restoring registers,
//adjusting the stack, and a call to some monitoring function
__declspec(naked)
unsigned __int64 __cdecl rdtsc(void)	//The __cdecl is probably redundant in most cases
{
  __asm
  {
    rdtsc;
    ret;  // return value in EDX:EAX
  }
}

int main(int Argc, char *Argv[], char *Arge[])
{
	printf("Hi %d %s  ", Argc, *Argv);
	printf("%I64u  %I64u  ", getCycles(), rdtsc());
	Sleep(10000);
	TNEW = rdtsc();
	printf("%I64u\n", TNEW - TOLD);
	TOLD = TNEW;
	if (Argc) main(--Argc, ++Argv, Arge);
	else
		while (*Arge) printf("%s ", *Arge++);
	return 0;
}

// Don't use this form... too much overhead. See rdtsc above
unsigned __int64 getCycles()
{
  __asm {

        // useful sometimes to force completion of previous instructions
        // CPUID
        RDTSC;
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 4 thanks,but I think muti-thread debug... Pin
magicpapacy9-Feb-11 20:13
magicpapacy9-Feb-11 20:13 
GeneralAn astute observation. Also, RDTSC is implemented differentl... Pin
LaRoy Tymes8-Feb-11 6:14
LaRoy Tymes8-Feb-11 6:14 
GeneralYour proposal should be Ok if you impose a CPU affinity. The... Pin
tarzan le plombier7-Feb-11 3:51
tarzan le plombier7-Feb-11 3:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.