Jump to content

Runtime

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts
I want to calculate the runtime, but the result is always 0


int main()

{

    time_t inicio;

    time_t fim;

    int v[5] = {3,4,3,5,4};

    int i;

    inicio = time(NULL);

    for(i=0;i<5;i++)

                 printf("%d \n",v[i]);

    fim = time(NULL);

    printf("Tempo = %f \n",difftime(fim, inicio));

    system("PAUSE");	

    return 0;

}



#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
The runtime for that is likely to be measured in thousandths of a second, if that.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts

WingedPanther said:

The runtime for that is likely to be measured in thousandths of a second, if that.


int main()

{

    time_t inicio;

    time_t fim;

    int v[5] = {3,4,3,5,4};

    int i;

    inicio = time(NULL);

    for(i=0;i<5;i++)

                 printf("%d \n",v[i]);

    fim = time(NULL);

   [B] printf("Tempo = %f \n",(difftime(fim, inicio))/1000);[/B]

    system("PAUSE");	

    return 0;

}


But the result is 0

#4
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts
Even if I convert to thousandths of seconds, seconds or hours the result is 0

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
My guess: difftim(fim, inicio) is less than 1000, so dividing by 1000 results in 0.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts

WingedPanther said:

My guess: difftim(fim, inicio) is less than 1000, so dividing by 1000 results in 0.

If I do not divide is also 0

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
time_t has a limited resolution. Your program may be running too fast for it to measure.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts

WingedPanther said:

time_t has a limited resolution. Your program may be running too fast for it to measure.

How can I increase the limit?

#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
The exact definition is implementation defined. That can include limits on the OS.

You can also try using clock_t, clock(), and CLOCKS_PER_SEC.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog