Dev - C++ - Threads - Con la función time, medir el tiempo que tarda el programa

 
Vista:
Imágen de perfil de rocio
Val: 50
Ha mantenido su posición en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

Threads - Con la función time, medir el tiempo que tarda el programa

Publicado por rocio (18 intervenciones) el 25/09/2020 23:29:08
Alguno me da una mano? ando perdidisima Q.Q

●Con la función time, medir el tiempo que tarda el programa anterior.
● Modificar el programa anterior para que cada una de las 5 llamadas a la función do_nothing() se ejecute por un thread


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h> //incluimos la libreria de estandar input/output
#include <unistd.h> //para hacer sleep
#include <time.h> //para inicializar el tiempo
 
void do_nothing (int microseconds){
usleep(2000000); //esperar 2 segundos, 1 millon de microsegundos en 1 segundo
//dormir el proceso, simula que esta haciendo alguna tarea
}
 
int main() {
do_nothing();
do_nothing();
do_nothing();
do_nothing();
do_nothing();
return 0;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Alfil
Val: 4.344
Oro
Ha mantenido su posición en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

Threads - Con la función time, medir el tiempo que tarda el programa

Publicado por Alfil (1444 intervenciones) el 26/09/2020 23:40:05
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <windows.h>    // Sleep
#include <time.h>       // clock
 
void do_nothing()
{
    Sleep(2000);
}
 
int main()
{
    unsigned t0, t1;
 
    t0 = clock();
 
    do_nothing();
    do_nothing();
    do_nothing();
    do_nothing();
    do_nothing();
 
    t1 = clock();
 
    double time = (double(t1-t0) / CLOCKS_PER_SEC);
 
    printf("\nTiempo transcurrido: %f\n", time);
 
    return 0;
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
Imágen de perfil de rocio
Val: 50
Ha mantenido su posición en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

Threads - Con la función time, medir el tiempo que tarda el programa

Publicado por rocio (18 intervenciones) el 26/09/2020 23:44:55
lo logre sacar a esto muchas gracias
ando con la otra parte que dice
● Modificar el programa anterior para que cada una de las 5 llamadas a la función do_nothing() se ejecute por un thread

ya me duele la cabeza intentando xd
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
-1
Comentar
Imágen de perfil de Alfil
Val: 4.344
Oro
Ha mantenido su posición en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

Threads - Con la función time, medir el tiempo que tarda el programa

Publicado por Alfil (1444 intervenciones) el 27/09/2020 09:47:58
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <windows.h>    // Sleep
#include <time.h>       // clock
#include <pthread.h>    // pthread
 
void *do_nothing(void *arg)
 
{
    Sleep(2000);
}
 
int main()
{
    unsigned t0, t1;
    pthread_t thread1, thread2, thread3, thread4, thread5;
 
    pthread_create(&thread1, NULL, do_nothing, NULL);
    pthread_create(&thread2, NULL, do_nothing, NULL);
    pthread_create(&thread3, NULL, do_nothing, NULL);
    pthread_create(&thread4, NULL, do_nothing, NULL);
    pthread_create(&thread5, NULL, do_nothing, NULL);
 
    t0 = clock();
 
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    pthread_join(thread3, NULL);
    pthread_join(thread4, NULL);
    pthread_join(thread5, NULL);
 
    t1 = clock();
 
    double time = (double(t1-t0) / CLOCKS_PER_SEC);
 
    printf("\nTiempo transcurrido: %f\n", time);
 
    return 0;
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar