سؤال

#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

#include <netinet/in.h>
#include <arpa/inet.h>

typedef unsigned int uint32;

#define million 1000000L

long duration2ms, duration10ms, duration100ms;
double Task2ms_Raster, Task10ms_Raster, Task100ms_Raster;
timer_t firstTimerID, secondTimerID, thirdTimerID;

void TASK1(Task2ms_Raster) {
    struct timespec start, stop;
    int a, b, c;
    uint32 StartTime, StopTime;
    a=100, b=2;

    if((StartTime = clock_gettime(CLOCK_REALTIME, &start)) == -1) {
        perror("clock gettime");
    }
    printf("start time is= %ld\n", StartTime);
    //I am performing some computation like 
    a = a + 100;
    b = a + 120;
    a = a + b;
    b = a;
    c = b;
      if((StopTime = clock_gettime( CLOCK_REALTIME, &stop)) == -1) {
        perror("clock gettime");
    }

 printf("stop time is= %ld\n", StopTime);

    duration2ms = (stop.tv_sec - start.tv_sec) +
                  (double)(stop.tv_nsec - start.tv_nsec) /
                  (double)million;
    printf("time difference is= %ld\n", duration2ms);
}

I created the timer and calling the task for every 2ms, 10ms and 100ms. The above is the code for 2ms task. I want to calculate the start time and stop time for performing some computation within the task and finally want to know the time difference between that. when I run my application, it is just displaying the time difference (i.e duration2ms). It is not printing the StartTime and StopTime. Could someone please help me.

هل كانت مفيدة؟

المحلول

If you read the manual page of clock_gettime you would see that

clock_gettime() return 0 for success, or -1 for failure.

So, you are printing the return code only. To print start time and stop time you will need to print the content of variables start and stop. For example:

   printf("Start time: %d.%09d\n", start.tv_sec, start.tv_nsec);

نصائح أخرى

what u need is

uint64_t u64StartTime = (start.tv_sec * 100000000) + start.tv_nsec;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top