By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,233 Members | 1,790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,233 IT Pros & Developers. It's quick & easy.

using QueryPerformance

P: n/a
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;
}

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
wallacej wrote:
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
Jul 23 '05 #2

P: n/a
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?

Jul 23 '05 #3

P: n/a
wallacej wrote:
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
Jul 23 '05 #4

P: n/a
On 23/2/05 3:50 AM, wallacej wrote:
//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.
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.