473,324 Members | 2,511 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,324 software developers and data experts.

Time my code

I have code that looks like this:

Expand|Select|Wrap|Line Numbers
  1. while (1)
  2. {
  3.      PostSemaphore(mysem);
  4. }
  5.  
I want to be able to simulate a 2KHz interrupt in windows. I just want to add a line in my loop to tell windows to execute my code 2000 times in a second.

Is there a way?

Thanks in advance,
Sevak.
Oct 8 '08 #1
11 1847
Banfa
9,065 Expert Mod 8TB
No there is no way in C (or Windows) to say execute code x times per second. However you can use a timer of some sort.

Since 2000Hz equates to every 0.5ms you can not use the Sleep function to just put your thread to speel for the required time as that using units of ms.

However you could use a waitable timer in Windows if your hardware is up to running a timer at that frequency.

Google Waitable Timer Objects
Oct 9 '08 #2
Thanks for the help.

I finally got to the point where I can give this a shot.

Sevak
Oct 23 '08 #3
TamusJRoyce
110 100+
I know of very few programs that run in the 2mhz range. Even on my old 286 16 Mhz computer this would be achievable.

It may not be an accurate way to do this, but I'm sure everyones processor is accurate to within 10 Mhz. You do have to think about multi-threading and such, but hey. It doesn't hurt to try this method.

Our goal is to seem like we run 2048 instructions a second.

Expand|Select|Wrap|Line Numbers
  1. #include <time.h>
  2.  
  3. #define NUMBER_OF_AVG 10
  4.  
  5. void foo() 
  6. {
  7.   // Your Code
  8. } // End foo() Function
  9.  
  10.  
  11. int main()
  12. {
  13. clock_t begin, end, avg;
  14. int instructionCount = 0;
  15.  
  16. avg = 0;
  17. for (int count = 0; count < NUMBER_OF_AVG; ++count)
  18. {
  19.   begin = clock();
  20.   do {
  21.     ++instructionCount;
  22.   } while(instructionCount < 2048)
  23.   end = clock();
  24.  
  25.   avg += (end - begin);
  26. } // End for
  27. avg = avg / NUMBER_OF_AVG;
  28.  
  29. // Divide by three since three instructions are "supposedly"
  30. //   being done.  You can quickly see how inaccurate this is.
  31. clock_t timeToExecuteTwoMHz = avg / 3;
  32.  
  33. // If you could accurately tell how many instructions are being 
  34. //   ran in a second, this program would be accurate.
  35. //   But different instructions take different time.  So unless
  36. //  there is an OS specific method of threading your program
  37. //  as a realtime processor-thread at 2 MHz, it will be hard
  38. //  to accuratly do this in software (to my knowledge)
  39.  
  40. avg = 0;
  41. for (int count = 0; count < NUMBER_OF_AVG; ++count)
  42. {
  43.   begin = clock();
  44.   foo();
  45.   end = clock();
  46.  
  47.   avg += (end - begin)
  48. } // End for
  49. clock_t timeToRunFoo = avg / NUMBER_OF_AVG;
  50.  
  51. clock_t timeToWait = timeToExecuteTwoMHz - timeToRunFoo;
  52.  
  53. // Looping to run foo() continuously at 2 Mhz
  54. while (true)
  55. {
  56.   begin = clock();
  57.   foo();
  58.  
  59.   // printf will probably take longer than 2MHz to run?
  60.   // printf("Your program took %i extra time to execute \n",
  61.   //        (int)(timeToRunFoo - (clock() - begin)));
  62.  
  63.   while ((clock() - begin) < timeToExecuteTwoMHz)
  64.   {
  65.     // Nothing Needed
  66.   } // End while
  67. } // End while
  68.  
Oct 24 '08 #4
TamusJRoyce
110 100+
Agg... I just saw PostSemaphore(mysem); which is a low-level system call (well, semaphores are at least). Well, I know my software one won't work. But was fun to write.

Have you found anything like a sleep() or sleepThread() Method? It's been a while since I've used pthreads, so if that's the threading library, I'm trying to think of a way to do this.

Hopefully you see it in your book/source
Oct 24 '08 #5
One way that I thought was a good approach was to use the system clock. I created (inherited) a function which gets passed a certain time (seconds), and it loops until the condition has been met.

here is what it looks like.

void wait(int seconds)
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}

....and it works!

Now, all I have to do is to create another Semaphore and pend on that semaphore till time has been met.

What do you think?

Sevak
Oct 24 '08 #6
JosAH
11,448 Expert 8TB
What do you think?
That approach only gives you a granularity of a second; not very usefull if you
want a granularity of 0.5ms.

kind regards,

Jos
Oct 24 '08 #7
can't I pass in 0.0005?

Sevak
Oct 24 '08 #8
JosAH
11,448 Expert 8TB
can't I pass in 0.0005?

Sevak
Check out what your CLOCKS_PER_SEC value is; that's how many ticks per
second your clock() value will change. The way you implemented your function
has a granularity of one second (1000ms)

kind regards,

Jos
Oct 24 '08 #9
oops.

Nevermind.

Thanks,
Sevak.
Oct 24 '08 #10
I am trying to use Waitable Timer in windows and I can't figure out how to control the time.

All I want is for my while loop to pause for a certain time, in order for my code to only execute roughly aournd 2000 times in a second.

while(1)
{
//run code 2000 times in a second.
}

I definitley do not want to run the code 2000 times in bursts. For example, I dont want the code to run 2000 times and wait until a second has gone by. The current loop counter reaches 42,000 before a second before a second has even gone by. What I want is the code to loop and at the same time have small pauses in between to get it to reach 2000 cycles within a second.

And what the heck is liDueTime.QuadPart???

Sorry, I have been working on this for a while. I wish Sleep() method was not in terms of ms (I would have been done by now).

Thanks,
Sevak
Oct 24 '08 #11
Using this code

I got it to run 2000 times in 2 seconds. All I need to do now is to bring it down to 1 second and I am DONE!!

Expand|Select|Wrap|Line Numbers
  1. HANDLE hTimer = NULL;
  2. LARGE_INTEGER liDueTime;
  3.  
  4. main()
  5. {
  6.      cycles = 0;
  7.      liDueTime.QuadPart=-1;
  8.  
  9.      hTimer = CreateWaitableTimer(NULL, TRUE, "WaitableTimer");
  10.  
  11.      while (1)
  12.      {
  13.        ........
  14.        SetWaitableTimer(hTimer, &liDueTime, 0, NULL, NULL, 0);
  15.        WaitForSingleObject(hTimer, INFINITE);
  16.        cycles++;
  17.      }
  18.  
  19. ...........
  20. }
  21.  
Oct 24 '08 #12

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

Similar topics

17
by: newbiecpp | last post by:
I have hard time to understand run-time environment. Let assume that I have a program that has a simple variable alpha. When this variable is statically allocated, the compiler can use the...
6
by: cournape | last post by:
Hi there, I have some scientific application written in python. There is a good deal of list processing, but also some "simple" computation such as basic linear algebra involved. I would like to...
3
by: luscus | last post by:
Thanks for all the responses on my first question. Unfortunately the answers I was given were too complicated for my small brain , and neophite condition to understand. So if you could talk down to...
0
by: Edward Diener | last post by:
In Borland's VCL it was possible to divide a component into design time and run time DLLs. The design time DLL would only be necessary when the programmer was setting a component's properties or...
15
by: Khurram | last post by:
I have a problem while inserting time value in the datetime Field. I want to Insert only time value in this format (08:15:39) into the SQL Date time Field. I tried to many ways, I can extract...
1
by: OleMacGeezer | last post by:
Hello Everyone, I am a brand new Python programmer with barely a month of experience under my belt. Here are my specs: Mac OSX Panther 10.3.9 Jython 2.1 implementation with Hermes BBS...
5
by: chaos | last post by:
Hi , I stuck in my project as i face a problem How to show the available time of the venue when i select the day?? Need to connect to the database to find out the available time of the venue on...
0
yasirmturk
by: yasirmturk | last post by:
Standard Date and Time Functions The essential date and time functions that every SQL Server database should have to ensure that you can easily manipulate dates and times without the need for any...
15
by: student4lifer | last post by:
Hello, I have 2 time fields dynamically generated in format "m/d/y H:m". Could someone show me a good function to calculate the time interval difference in minutes? I played with strtotime() but...
0
amitpatel66
by: amitpatel66 | last post by:
Hi All, Find below some useful information about Time Zone Conversion in oracle. Hope this would be helpful for many of them since all the real time projects that we work in follow different time...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.