0

I want to measure how much time a query cost, so I have code below:

struct timespec vartime = timer_start();  
res=PQexec(conn, "...");
long time_elapsed_nanos = timer_end(vartime);
printf("Time taken (ns), %ld\n",time_elapsed_nanos);

however, the output is very small, say, 0.05ms. But in terminal, if I use explain analyze it shows 400ms.

why I could not measure the cost using code above? what should I do? thanks,

user3329081
  • 337
  • 4
  • 15

1 Answers1

0

The timer_start() and timer_end() functions are not part of the C standard library, and it's unclear what the first one does. struct timespec holds an elapsed time, so having timer_start() returning that type is suspicious. Since what other point in time would it be counting?

I'd suggest to use a piece of code based on gettimeofday(), as shown in one answer of:

Calculating elapsed time in a C program in milliseconds

PQexec() is a blocking call, so measuring the time will work the same as for some long CPU calculation that the above technique uses as a demo.

Community
  • 1
  • 1
Daniel Vérité
  • 58,074
  • 15
  • 129
  • 156