maeDaSol Posted November 24, 2007 at 05:34 PM Report Share #149970 Posted November 24, 2007 at 05:34 PM olá tenho estado às voltas com um programa que tem que medir o tempo que demora a multiplicar matrizes. mas dá-me sempre 0 segundos. Alguém sabe como é que meço o tempo em milissegundos ou outra unidade mais pequena que os segundos? o meu código é mais ou menos este: time_t tempo1, tempo2; time(&tempo1); matrizR = multiplicar(d, matrizA, matrizB, matrizR); time(&tempo2); printf("tempo de execução: %.2d \n", (difftime(tempo2, tempo1))); brigada 😁 Link to comment Share on other sites More sharing options...
Betovsky Posted November 24, 2007 at 10:28 PM Report Share #150072 Posted November 24, 2007 at 10:28 PM Isso é porque a função time só conta em segundos. A tua operação sobre as matrizes deve demorar menos que 1 segundo. Já estou um pouco ferrujado de C, mas acho que o que querias era a função clock que te dá o tempo em termos de processador usado. O valor retornado pela função é em ciclos/segundo que varia de sistema para sistema, ou seja, terias que ver o valor de CLOCK_PER_SECOND para saber o valor dele. E depois era uma questão de fazer contas... "Give a man a fish and he will eat for a day; Teach a man to fish and he will eat for a lifetime. The moral? READ THE MANUAL !" Sign on a computer system consultant's desk Link to comment Share on other sites More sharing options...
rgcaldas Posted November 24, 2007 at 11:59 PM Report Share #150102 Posted November 24, 2007 at 11:59 PM Para contares abaixo de 1 segundo podes usar a função gettimeofday http://www.opengroup.org/onlinepubs/009695399/functions/gettimeofday.html e precisas da estrutura timeval http://www.gnu.org/software/libc/manual/html_node/Elapsed-Time.html Link to comment Share on other sites More sharing options...
mogers Posted November 25, 2007 at 12:01 AM Report Share #150105 Posted November 25, 2007 at 12:01 AM eu costumo usar: clock_t start = clock(); matrizR = multiplicar(d, matrizA, matrizB, matrizR); clock_t ends = clock(); printf("Tempo de execucao: %.5f seg\n\n",(double) (ends - start) / CLOCKS_PER_SEC); 😛 "What we do for ourselves dies with us. What we do for others and the world, remains and is immortal.", Albert Pine Blog pessoal : contém alguns puzzles, algoritmos e problemas para se resolver com programação. Link to comment Share on other sites More sharing options...
maeDaSol Posted November 30, 2007 at 03:34 PM Author Report Share #151371 Posted November 30, 2007 at 03:34 PM a muito custo o meu prof lá falou dessa função timeofday. vou exper essa e tb vou exp o clock. brigada 😉 Link to comment Share on other sites More sharing options...
maeDaSol Posted December 3, 2007 at 11:33 AM Author Report Share #151913 Posted December 3, 2007 at 11:33 AM oi, consegiu por isto a funcionar aqui vai o código final struct timeval t1,t2; ( ...) gettimeofday(&t1, NULL); matrizR = multiplicar(d, matrizA, matrizB, matrizR); gettimeofday(&t2, NULL); printf("%.2f segundos\n", (float)(t2.tv_usec-t1.tv_usec)/1000); //calcular a diferença de tempo (em miliseg) e converter para float obrigada pela ajuda, deu mt jeito 🙂 Link to comment Share on other sites More sharing options...
rgcaldas Posted December 3, 2007 at 10:31 PM Report Share #152112 Posted December 3, 2007 at 10:31 PM oi, consegiu por isto a funcionar aqui vai o código final struct timeval t1,t2; ( ...) gettimeofday(&t1, NULL); matrizR = multiplicar(d, matrizA, matrizB, matrizR); gettimeofday(&t2, NULL); printf("%.2f segundos\n", (float)(t2.tv_usec-t1.tv_usec)/1000); //calcular a diferença de tempo (em miliseg) e converter para float obrigada pela ajuda, deu mt jeito 😉 Atenção que assim só funciona as vezes. O membro usec guarda o numero de micro-segundos entre dada segundo não o tempo todo tipo se a primeira chamada à funções gettimeofday devolver 1001 para o tv_sec e 599999 para o tv_usec quer dizer que está quase a passar o segundo, se o calculo da matrix demorar 10 mili-segundos a segunda chamada devovel tv_sec=1002 e tv_usec = 009999, e se tentares fazer a conta como tens vai dar negativo. deves fazer algo do género (double)((t2_sec+(t2.tv_usec/1000000))-(t1_sec+(t1.tv_usec/1000000))) Assim dá-te os segundos com precisão de micro-segundos (nas casas decimais) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now