Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <sys/time.h>
- const int DIM=1000;
- float Nmassimo;
- float Nminimo;
- float Nsomma;
- float array[1000][1000];
- //TEMPO
- long long current_timestamp() {
- struct timeval te;
- gettimeofday(&te, NULL); // get current time
- long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // caculate milliseconds
- // printf("milliseconds: %lld\n", milliseconds);
- return milliseconds;
- }
- //TEMPO
- void *massimo(void *ptr)
- {
- Nmassimo=-20001;
- int i=0,j=0;
- for(i=0; i<DIM; i++)
- {
- for(j=0; j<DIM; j++)
- {
- if(array[i][j]>Nmassimo)
- Nmassimo=array[i][j];
- }
- }
- }
- void *minimo(void *ptr)
- {
- Nminimo=20001;
- int i=0,j=0;
- for(i=0; i<DIM; i++)
- {
- for(j=0; j<DIM; j++)
- {
- if(array[i][j]<Nminimo)
- Nminimo=array[i][j];
- }
- }
- }
- void *somma(void *ptr)
- {
- Nsomma=0;
- int i=0,j=0;
- for(i=0; i<DIM; i++)
- {
- for(j=0; j<DIM; j++)
- {
- Nsomma+=array[i][j];
- }
- }
- }
- int main()
- {
- long long tempo1=current_timestamp();
- pthread_t tMassimo, tMinimo,tSomma;
- srand (time(NULL));
- int i=0,j=0;
- for(i=0; i<DIM; i++)
- {
- for(j=0; j<DIM; j++)
- {
- array[i][j]=(rand() % 40001)+(-20000);
- }
- }
- pthread_create( &tMassimo, NULL, massimo, NULL);
- pthread_create( &tMinimo, NULL, minimo, NULL);
- pthread_create( &tSomma, NULL, somma, NULL);
- pthread_join( tMassimo, NULL);
- pthread_join( tMinimo, NULL);
- pthread_join( tSomma, NULL);
- printf("Numero massimo: %f \n",Nmassimo);
- printf("Numero minimo: %f \n",Nminimo);
- printf("Somma: %f \n",Nsomma);
- long long tempo2=current_timestamp();
- printf("Tempo impiegato: %lli millisencondi \n",(tempo2-tempo1));
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement