How can I get the time to load and run a program?

Asked

Viewed 387 times

-1

How can I get time to load and run a program?

I want to know the time of loading and execution of a program, since the momentum where the user gives order to run it and the operating system makes the allocation in RAM until the time when the process is terminated.

  • If the community thinks it’s a duplicate of this question, can close. But I understand that there are two different questions. Mainly the calculation of the execution time of a program, since if the program itself is responsible for calculating its own execution time, it will influence the final result, leaving the result inaccurate.

  • Do you want to know the complexity of the algorithm or the actual time it takes? The question seems to be too generic. I don’t know what to say.

  • Sorry, but I can’t distinguish between the two terms: "Algorithm complexity" and "effective time". : S

  • Algorithm complexity is only the function itself, and the effective time would be the total time of the program (including process creation, RAM allocation, etc.)?

  • I thought you knew, after all you used a tag with the term. http://answall.com/q/33319/101. Effective time is the measured time.

  • No no, I’m still studying... (Thanks for the link). I want to measure the effective time, the total time of the program.

  • As you yourself mentioned, I understand you are duplicate of this, also yours: http://answall.com/questions/55087/ - I know you are asking for two different approaches, but in the end the answer is the same: you take the system time at the beginning and end of the program, and calculate the difference. It is negligible to capture this time, as long as you do only once at the beginning and once at the end (without putting extra code inside loops).

  • I found what I needed here. Imagine a large program, which takes time to load into memory. The loading time will no longer be negligible. I was unhappy in my question, I should have asked the time of the process. Sorry for the confusion. The program I put in the answer serves what I need. Feel free to modify my answer.

Show 3 more comments

1 answer

2


The following code does what you need:

/*
Compilacao: gcc tempo.c -o tempo

Execucao: tempo <comando>
      onde <comando> pode ser ls por exemplo

Sugestao de Modificacoes:
1. Retire a chamada wait(NULL) e observe o comportamento
2. Faca com que cada processo imprima o seu pid
   o pid 'e obtido chamando-se getpid()
*/

#include <stdio.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/types.h>

main(int argc, char *argv[])
{
  int pid;
  struct timeval tv1, tv2;
  double t1, t2;

  if(argc != 2)
  {
    printf("Uso: tempo algum_comando\n");
    exit(1);
  }

  pid = fork();                                /* fork */
  if(pid == 0) {
    printf("\nProcesso filho vai executar %s agora.\n",argv[1]);
    gettimeofday(&tv1, NULL);   /* processo pai continua ... */
    t1 = (double)(tv1.tv_sec) + (double)(tv1.tv_usec)/ 1000000.00;
    system(argv[1]);             /* processo filho executa comando */
    gettimeofday(&tv2, NULL);
    t2 = (double)(tv2.tv_sec) + (double)(tv2.tv_usec)/ 1000000.00;
    printf("\nExecucao de %s terminou agora.\n",argv[1]);
    printf("\nO tempo de execucao de %s por filho foi: %lf\n", 
    argv[1], (t2 - t1));
  }
  else{  
    gettimeofday(&tv1, NULL);   /* processo pai continua ... */
    t1 = (double)(tv1.tv_sec) + (double)(tv1.tv_usec)/ 1000000.00;
    /*    wait(NULL); */            /* sincroniza com o termino do filho */
    gettimeofday(&tv2, NULL);
    t2 = (double)(tv2.tv_sec) + (double)(tv2.tv_usec)/ 1000000.00;
    printf("\nO tempo de execucao de processo pai foi: %lf\n",(t2 - t1));
  }
}

Example-based: http://www.dca.fee.unicamp.br/~lotufo/cursos/EA877/sistemas-operativos/exemplos/tempo. c

  • 1

    This code is complicated for what you asked for at least two reasons: First, that Fork() is not cross-platform, and second, that this technique will account for not only runtime, but the hard drive load and all allocations and releases of memory and resources at the beginning and end of the program (which may or may not be what you want, it would be the case to specify in the question). I still believe that the other question will give you something more concrete for what you need.

  • It was just what I needed, he only finishes counting the time when the program is shut down. I think I modified my question to make this clear, I apologize for the confusion. But feel free to modify my question.

  • 3

    There is no reason to apologize, nor do I believe that it is the case to tamper with the question. If this is the solution, you can look for the timeit.exe for Windows, or use Measure-Command of Powershell, and on Linux we have the time, that already makes this measurement. It would not even be a programming problem, so I find more promising the other question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.