473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

GetTickCount() not updating

1
Hi Guys,

I got an assignment to calculate the execute time difference a code written with threads and one without. I got the algorithm working but GetTickCount() return the same number before and after the code runs. Any help is much appreciated!

When I run my code, I get
13611602
13611602
# of prime < 10000 is 1229

0 ms elapsed time

# of prime < 10000 is 1229

0 ms elapsed time

Why doesn't GetTickCount() update itself after running the for loop to count the number of prime #s < 1000? Thanks.

I've copied the code below:

// Prime.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "Windows.h"
#include "LIMITS.h"
#include <iostream>

using namespace std;

static HANDLE Thread_semaphore;
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hThread1;
DWORD dwThread1ID = 0;
HANDLE hThread2;
DWORD dwThread2ID = 0;
HANDLE hThread3;
DWORD dwThread3ID = 0;
HANDLE hThread4;
DWORD dwThread4ID = 0;
INT nParameter = 1;
DWORD WINAPI PerfectThread (LPVOID lpArg);
DWORD WINAPI PrimeThread (LPVOID lpArg);
DWORD time_count;
unsigned long i, j, sum;
printf("\n");
int pcount = 0;
int pstart = 2; // Start from 2
int pend = 10000;
time_count = GetTickCount();
cout << time_count << endl;
for(;pstart <= pend; pstart++)
{
int trialDivisor = 2;
int prime = 1;
for(; trialDivisor * trialDivisor <= pstart; trialDivisor++)
{
if(pstart % trialDivisor == 0)
{
prime = 0;
break;
}
}
if(prime) pcount++;
}
cout << GetTickCount() << endl;
time_count = GetTickCount() - time_count;
printf("# of prime < %d is %d", pend, pcount);
printf("\n\n %d milliseconds elapsed time\n\n\r", time_count);
Sleep(1000);
// do it with threads
Thread_semaphore = CreateSemaphore(NULL, 0, 4, TEXT("Thread_Done"));
time_count = GetTickCount();
hThread1 = CreateThread( NULL, 0, PrimeThread, (LPVOID)nParameter, 0, &dwThread1ID);
nParameter++;
hThread2 = CreateThread( NULL, 0, PrimeThread, (LPVOID)nParameter, 0, &dwThread2ID);
nParameter++;
hThread3 = CreateThread( NULL, 0, PrimeThread, (LPVOID)nParameter, 0, &dwThread3ID);
nParameter++;
hThread4 = CreateThread( NULL, 0, PrimeThread, (LPVOID)nParameter, 0, &dwThread4ID);
WaitForSingleObject(Thread_semaphore, INFINITE);
WaitForSingleObject(Thread_semaphore, INFINITE);
WaitForSingleObject(Thread_semaphore, INFINITE);
WaitForSingleObject(Thread_semaphore, INFINITE);
time_count = GetTickCount() - time_count;
printf("\n\n %d milliseconds elapsed time\n\r", time_count);
Sleep(1000);
CloseHandle(hThread1);
CloseHandle(hThread2);
CloseHandle(hThread3);
CloseHandle(hThread4);

getchar();
return 0;
}

DWORD WINAPI PrimeThread (LPVOID lpArg)
{
int pend = 10000;
int pstart = 2;
int pcount = 0;
INT threadnumber = (INT) lpArg;
unsigned long i, j, sum;
for (j=1 + threadnumber; j<=10000; j=j+4)
{
for(;pstart <= pend; pstart++)
{
int trialDivisor = 2;
int prime = 1;
for(; trialDivisor * trialDivisor <= pstart; trialDivisor++)
{
if(pstart % trialDivisor == 0)
{
prime = 0;
break;
}
}
if(prime) pcount++;
}
}
printf("# of prime < %d is %d", pend, pcount);
ReleaseSemaphore(Thread_semaphore, 1, NULL);
return 0;
}
Nov 29 '08 #1
1 4074
vmpstr
63
What might be happening is that your program runs too fast for it to update, due to its limited resolution.

You can debug the problem by putting a call to Sleep before the second GetTickCount, to see if the difference is now not 0.

If that's the case, then look up GetTickCount on msdn. They have a link to high resolution timers there, which might help.

Unfortunatelly, I don't have any first hand experience with this, so I can't really help further.

good luck
Dec 1 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Jason | last post by:
Let's say I have an html form with 20 or 30 fields in it. The form submits the fields via POST to a php page which updates a table in a database with the $_POST vars. Which makes more sense? ...
2
by: Chua Wen Ching | last post by:
Hi there. I think in vb6, i can use GetTickCount easily by accessing the user32.dll (hmm.. can't remember what is the dll name, but something like that) Is there any code in the...
1
by: Dark74 | last post by:
Vladimir Nesterovsky wrote: > If you realy need to measure short time use rathe > QueryPerformanceCounter > and QueryPerformanceFrequency instead of GetTickCount. > -- > Vladimir Nesterovsky >...
10
by: jaYPee | last post by:
does anyone experienced slowness when updating a dataset using AcceptChanges? when calling this code it takes many seconds to update the database SqlDataAdapter1.Update(DsStudentCourse1)...
2
by: farsad nasseri | last post by:
Hi We're having some timing issues with one of our longer running software programs. I am trying to measure how many times my computer can loop through before one millisecond has passed by and...
14
by: el_sid | last post by:
Our developers have experienced a problem with updating Web References in Visual Studio.NET 2003. Normally, when a web service class (.asmx) is created, updating the Web Reference will...
23
by: EOS | last post by:
Hi, As the title indicated, does anyone know the equivalent of GetTickCount() function in C/C++ for C#? The function is supposed to return time elapsed since the computer/device is booted in...
2
by: Don Miller | last post by:
Can I call GetTickCount straight from ASP? If so how do I do it? Thanks. Don
5
by: rosaryshop | last post by:
I'm working a jewelry/rosary design web site at http://www.rosaryshop.com/rosariesAndKits2.php. As the user makes selections, it updates images of various parts, giving them a preview of the...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.