0
#include <stdio.h>
#include <time.h>
#include <windows.h>

void Task()
{
    printf("Hi");
}

int main ( ) {
    time_t t;
    clock_t start, end;
    long i;
    long count;
    double x = 0.0;
    count = 2;

    start = clock();

    time(&t);

    printf(ctime(&t));
    printf( "Counting to %ld\n", count );

    if(count)
    {
        Task();
    }

    end = clock();

    printf( "That took %f seconds and I counted up to %ld", (((double)(end-start)/CLOCKS_PER_SEC)), count );
    printf( "\nThat also took %d clock tics\n ", clock());
    return 0;
} 

I want to get the start time and end time taken to execute the Task function. I am trying to create interrupt for the Task function but displaying Hi in the program. I am not successful with that. So could you please anyone can guide me regarding this.

Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
sachin s
  • 7
  • 1
  • 6
  • 1
    There is no way of having interrupts in user-mode, only kernel-mode drivers can service interrupt requests. There's probably another solution (e.g., some kind of callback as the multimedia timers of Windows) but please expose your problem more in detail so that readers can exactly understand what you are trying to do... – Ale Nov 04 '13 at 15:40
  • Why would you want to create interrupt handler in Windows? – wilx Nov 04 '13 at 15:41
  • For every 2ms, 10ms and 100ms. there should be call to a specific function!!! In the above I have just trying to do for 2ms!!! – sachin s Nov 04 '13 at 15:41
  • Why do you want to call a function each 2 ms, etc.? – wilx Nov 04 '13 at 15:44
  • 2
    `if (2)` doesn't block your program for 2ms. – Lightness Races in Orbit Nov 04 '13 at 15:45
  • I have to call the Task function at 2ms!!! another example : have to call another task function at 10ms. How to do this ?? – sachin s Nov 04 '13 at 15:45
  • 4
    Sachin, you are only tell us what you want to do but you are not telling us why you want to do it and why do you think it is the right approach to solving your task. – wilx Nov 04 '13 at 15:50
  • There are three task and it should be called at different time. – sachin s Nov 04 '13 at 16:21

3 Answers3

1

Try starting with the Multimedia Timers. Another possible approach might be using CreateTimerQueueTimer() and friends.

wilx
  • 17,697
  • 6
  • 59
  • 114
  • How to calculate the start time and end time for the task ?? – sachin s Nov 04 '13 at 15:52
  • 1
    @sachins: Read the docs carefuly: `CreateTimerQueueTimer() - DueTime [in]` The amount of time in milliseconds _relative to the current time_ that must elapse before the timer is signaled for the first time. – wilx Nov 04 '13 at 15:54
  • could you please write me a code to calculate a start time and end time ?? – sachin s Nov 04 '13 at 15:59
0

There is no way of having interrupts in user-mode, only kernel-mode drivers can service interrupt requests.

However you can have a callback function called by the OS in a periodic way. On Windows you can achieve this using the multimedia times (however declared obsolete by Microsoft) or timer queue timers (check this, for example: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682485%28v=vs.85%29.aspx).

Here is an old test program I wrote that uses the Multimedia timers (obsolete but they still work on recent Windows versions...):

#include <stdio.h>
#include <windows.h>

volatile long timer = 0;

// Will be called every 1 ms
void CALLBACK timer_func(UINT uTimerID, UINT uMsg, DWORD *dwUser,
    DWORD *dw1, DWORD *dw2)
{
    timer++;
}

int main(int argc, char *argv[])
{
    MMRESULT id = timeSetEvent(1, 0, (LPTIMECALLBACK) timer_func, NULL, TIME_PERIODIC);
    printf("Waiting 10 seconds... ");
    fflush(stdout);
    Sleep(10000);
    printf("ok. Timer = %ld.\n", timer);
    timeKillEvent(id);
    return 0;
}

If you just want to precisely measure how long a function call lasts, just use QueryPerformanceCounter() and QueryPerformanceFrequency():

#include <windows.h>
#include <stdio.h>

void task()
{
    // do something...
}

int main()
{
    LARGE_INTEGER start, stop, freq;

    QueryPerformanceCounter(&start);
    task();
    QueryPerformanceCounter(&stop);
    QueryPerformanceFrequency(&freq);

    double time_len = (stop.QuadPart - start.QuadPart) / (double) freq.QuadPart;
    printf("Task length: %0.8f seconds.\n", time_len);
}
Ale
  • 1,727
  • 14
  • 26
  • How to calculate the start time and end time for the task ?? – sachin s Nov 04 '13 at 15:54
  • I added an example of how to have a callback function called periodically (for instance every 1 ms in the example code). Then what you do inside the function depends on your application... – Ale Nov 04 '13 at 16:00
  • After the task(void CALLBACK timer_func(UINT uTimerID, UINT uMsg, DWORD *dwUser,DWORD *dw1, DWORD *dw2) in the above case starts executing. the body of the function could be anything!!! question : how to calculate start time of the process and end time of the process ?? – sachin s Nov 04 '13 at 16:11
  • sorry but I don't understand your question... what are you exactly trying to do? Measuring how much time something (your task?) lasts? But in this case, why do you need an "interrupt" function? Could you please explain more in detail what you are looking for? – Ale Nov 04 '13 at 16:14
  • how to calculate the timestamp ?? – sachin s Nov 04 '13 at 16:16
  • I will interrupt at specific time and later when the task starts executing, how to calculate the time of executing the task and end of execution. Timestamp = endtime - start time; – sachin s Nov 04 '13 at 16:18
  • http://stackoverflow.com/questions/275004/c-timer-function-to-provide-time-in-nano-seconds?rq=1 – sachin s Nov 04 '13 at 16:22
  • can I use the above ideas from the link to calculate the time ?? – sachin s Nov 04 '13 at 16:22
  • Sure you can use for example QueryPerformanceCounter() and QueryPerformanceFrequency() to get very precise timing about how long a function is running. But I don't yet see what you want to do with your periodic "interrupt" function, and how it relates with the timing. If you just want to time a function, just call QueryPerformanceCounter() before and after, and then divide the difference by the value returned by QueryPerformanceCounter (casted to a double) to get the running time of your function in seconds. – Ale Nov 04 '13 at 16:24
  • Ale: I want to do that only. Could you please give me an example ?? – sachin s Nov 04 '13 at 17:03
  • I added an example of how to use those in my answer. – Ale Nov 04 '13 at 17:18
  • Ale : is it possible to calculate getstopwatch(); ?? – sachin s Nov 05 '13 at 15:13
  • what is getstopwatch()? what do you want this function to return, something like the elapsed time since a hypothetical startstopwatch()? – Ale Nov 05 '13 at 15:17
  • Ale: yes!! what you said is perfect. – sachin s Nov 05 '13 at 15:22
  • In a function they mentioned somthing like : 1: uint32 startTime = GetStopwatch(); last line : duration10ms = (uint32)( ( GetStopwatch() - startTime ) / STOPWATCH_TICKS_PER_US ); // I am not able to put the full code here!!! could you tell me what is that ?? – sachin s Nov 05 '13 at 15:28
  • They? Do you have a link? It looks like code for a microcontroller more than for a PC... (although this does not mean that there's no way of doing the same on a PC) – Ale Nov 05 '13 at 15:34
  • can I have your email id ?? I'll send that code across you !! – sachin s Nov 05 '13 at 15:39
  • you can send me a message on <1x2rl5gyovzyfx0@jetable.org> (valid only today) – Ale Nov 05 '13 at 15:46
  • Ok, received your code, actually this one: http://stackoverflow.com/questions/19638163/high-precision-event-timer. The GetStopWatch() function is doing, on its specific platform, exactly the same thing that QueryPerformanceCounter() does on Windows, the only differences being that it uses 32-bit integers instead of 64-bit, and that it gives the results in the return value rather than taking a pointer. The STOPWATCH_TICKS_PER_US would be basically equivalent to the value returned by QueryPerformanceFrequency() divided by 1000000 (as that function returns the number of ticks per second). – Ale Nov 05 '13 at 16:39
  • all the info needed about how to modify the example code should be in my previous comment... what else do you need to know? – Ale Nov 06 '13 at 09:40
  • Ale: The code sent to you is having a GetStopWatch() function. how to modify your code w.r.t. Could you PLEASE give me an example to modify your code to that ?? – sachin s Nov 06 '13 at 09:53
  • I cant modify the LARGE_INTEGER data type to any other data type uint64 then it will throw an error like : error C2224: left of Quadpart must have struct/union type. – sachin s Nov 06 '13 at 10:56
  • Ale: could you please help me ?? – sachin s Nov 06 '13 at 13:46
0

New answer after discussion (see comments of my previous answer): you can implement an equivalent to the GetStopWatch() function you want this way:

#include <windows.h>
#include <stdio.h>
#include <stdint.h>

// assuming we return times with microsecond resolution
#define STOPWATCH_TICKS_PER_US  1

uint64_t GetStopWatch()
{
    LARGE_INTEGER t, freq;
    uint64_t val;

    QueryPerformanceCounter(&t);
    QueryPerformanceFrequency(&freq);
    return (uint64_t) (t.QuadPart / (double) freq.QuadPart * 1000000);
}

void task()
{
    // do something...
}

int main()
{
  uint64_t start = GetStopWatch();
  task();
  uint64_t stop = GetStopWatch();

  printf("Elapsed time (microseconds): %lld\n", stop - start);
}

Hope this helps.

Ale
  • 1,727
  • 14
  • 26
  • #include #include #include // assuming we return times with microsecond resolution #define STOPWATCH_TICKS_PER_US 1 uint64_t GetStopWatch() { LARGE_INTEGER t, freq; uint64_t val; QueryPerformanceCounter(&t); QueryPerformanceFrequency(&freq); return (uint64_t) (t.QuadPart / (double) freq.QuadPart * 1000000); } task() { printf("hello\n"); } int main() { uint64_t start = GetStopWatch(); task(); uint64_t stop = GetStopWatch(); printf("Elapsed time (microseconds): %lld\n", stop - start); } – sachin s Nov 10 '13 at 18:24
  • If I call the task() function multiple times and it is printing hello. The (stop- start) value is different for calling task function multiple times. I am not getting the same elapsed time for calling it. – sachin s Nov 10 '13 at 18:49
  • This is normal, you could only assume constant timing for a function call if you're running it without any interrupts enabled (which is of course impossible on a multitasking OS as Windows, except if you run it inside a kernel driver). The time you see for your task() function call includes the time it takes to run the function itself, but also the time the system was running other processes or servicing interrupts. – Ale Nov 12 '13 at 08:17
  • I need your guidance. If I want to get the same elapsed time then what should I do ?? – sachin s Nov 12 '13 at 08:43
  • There are three tasks : task2, task10 and task100. I am using the create timer queue function to call the above three task from main function for every 2ms, 10ms and 100ms. Inside the void task2, void task10 and void task100() : I am using Query performance counter to calculate the elapsed time (I am using your code). But I am getting the different elapsed time for the task running multiple times. (The tasks are just printing hi(void task2), hello (void task10), hheh(void task100)) . if void task2 function is called first and I should get the same elapsed time during run time. – sachin s Nov 12 '13 at 08:50
  • Which elapsed time are you wanting to calculate? How long the single task() functions take? Or the time between them? Please be more clear in your questions, otherwise it's not possible to answer. – Ale Nov 13 '13 at 12:28
  • Elapsed time nothing but the stop-start time – sachin s Nov 13 '13 at 12:54
  • Ok but what are your start and stop times? The start and stop of each single task? – Ale Nov 14 '13 at 15:02