using QueryPerformance

W

wallacej

Hello

Can anybody explain to me why the following four FPRINTF lines of code
yield different results to their Console counterparts on the next line?
By my reckoning the results should be te same but one set are printed
to the console window and the other to a text file. The Console lines
are definitely creating correct results because they are a product of
help code for Microsoft Visual Studio .NET and the results look good.
The fprintf lines of code (my code) are definitely producing incorrect
results and all four of the outputs are often identical. Am I doing
some memory management incorrectly???

#include "stdafx.h"

#using <mscorlib.dll>
#include <tchar.h>
#include <windows.h>
#include <stdio.h>

using namespace System;

// This is the entry point for this application
int _tmain(void)
{
_int64 ctr1 = 0, ctr2 = 0, freq = 0;
int acc = 0, i = 0;
FILE *storage;

//Start timing the code
if(QueryPerformanceCounter((LARGE_INTEGER *)&ctr1) != 0)
{
storage = fopen("C:\\TestTimer.txt", "a"); //Open a file for data
output
//Code segment is being timed
for(i=0; i<100; i++) acc++;
//Finish timing the code
QueryPerformanceCounter((LARGE_INTEGER *)&ctr2);

fprintf(storage, "Start Value %i\n", ctr1.ToString());
Console::WriteLine("Start Value: {0}",ctr1.ToString());

fprintf(storage, "End Value %i\n", ctr2.ToString());
Console::WriteLine("End Value: {0}",ctr2.ToString());

QueryPerformanceFrequency((LARGE_INTEGER *)&freq);

fprintf(storage, "Frequency %i\n", freq.ToString());
Console::WriteLine(S"QueryPerformanceCounter minimum resolution:
1/{0} seconds.",freq.ToString());

fprintf(storage, "Time %i\n", ((ctr2-ctr1) * 1.0 /
freq).ToString());
Console::WriteLine("100 Increment time: {0} seconds.",((ctr2-ctr1) *
1.0 / freq).ToString());

fclose(storage);
}
return 0;
}
 
V

Victor Bazarov

wallacej said:
Can anybody explain to me why the following four FPRINTF lines of code
yield different results to their Console counterparts on the next line?

Probably because you're not using 'fprintf' correctly...
By my reckoning the results should be te same but one set are printed
to the console window and the other to a text file. The Console lines
are definitely creating correct results because they are a product of
help code for Microsoft Visual Studio .NET and the results look good.
The fprintf lines of code (my code) are definitely producing incorrect
results and all four of the outputs are often identical. Am I doing
some memory management incorrectly???
[...non-standard code removed mostly...]
fprintf(storage, "Start Value %i\n", ctr1.ToString());

What does 'ToString()' return? A const char*? Why are you printing it
with %i? Doesn't %i require a value of type 'int' (or convertible to it)?

Please forward your further inquiries about Windows API to a newsgroup
dedicated to Windows programming, comp.os.ms-windows.programmer.win32 or
to a microsoft newsgroup for .NET stuff: microsoft.public.dotnet.languages

V
 
W

wallacej

thanks for that, knew it might be a stupid mistake.
One more thing, do you know why the fprintf line of code would work in
one project but not in another?

I transfered the code to a cpp file in an existing project and i get an
error messagge
'left of 'To String' must have class/struct/union type'

Could this be because the original project is managed c++? and if so
how would i get around this?
 
V

Victor Bazarov

wallacej said:
thanks for that, knew it might be a stupid mistake.
One more thing, do you know why the fprintf line of code would work in
one project but not in another?

I transfered the code to a cpp file in an existing project and i get an
error messagge
'left of 'To String' must have class/struct/union type'

Could this be because the original project is managed c++? and if so
how would i get around this?

Yes, it definitely could. Managed C++ (a different language, really)
is off-topic here, sorry. Ask in a newsgroup dedicated to Visual C++
to learn more about it: microsoft.public.vc.language (or any other
microsoft.public.* that is relevant).

Good luck!

V
 
R

Richard Cavell

//Code segment is being timed
for(i=0; i<100; i++) acc++;

Also, to state the obvious, this piece of code does nothing and
Microsoft C++ will optimize it away if you allow it to. So this code
takes 0 time to execute.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top