SHOW:
|
|
- or go back to the newest paste.
| 1 | #include <sys/times.h> | |
| 2 | #include <time.h> | |
| 3 | #include <stdio.h> | |
| 4 | #include <pthread.h> | |
| 5 | ||
| 6 | long long int tmsBegin1,tmsEnd1,tmsBegin2,tmsEnd2,tmsBegin3,tmsEnd3; | |
| 7 | ||
| 8 | int array[100]; | |
| 9 | ||
| 10 | void *heavy_loop(void *param) {
| |
| 11 | int index = *((int*)param); | |
| 12 | int i; | |
| 13 | for (i = 0; i < 100000000; i++) | |
| 14 | array[index]+=3; | |
| 15 | } | |
| 16 | ||
| 17 | int main(int argc, char *argv[]) {
| |
| 18 | int first_elem = 0; | |
| 19 | int bad_elem = 1; | |
| 20 | int good_elem = 32; | |
| 21 | long long time1; | |
| 22 | long long time2; | |
| 23 | long long time3; | |
| 24 | pthread_t thread_1; | |
| 25 | pthread_t thread_2; | |
| 26 | - | pthread_t thread_3; |
| 26 | + | |
| 27 | tmsBegin3 = clock(); | |
| 28 | heavy_loop((void*)&first_elem); | |
| 29 | heavy_loop((void*)&bad_elem); | |
| 30 | tmsEnd3 = clock(); | |
| 31 | ||
| 32 | tmsBegin1 = clock(); | |
| 33 | pthread_create(&thread_1, NULL, heavy_loop, (void*)&first_elem); | |
| 34 | pthread_create(&thread_2, NULL, heavy_loop, (void*)&bad_elem); | |
| 35 | pthread_join(thread_1, NULL); | |
| 36 | pthread_join(thread_2, NULL); | |
| 37 | tmsEnd1 = clock(); | |
| 38 | ||
| 39 | tmsBegin2 = clock(); | |
| 40 | pthread_create(&thread_1, NULL, heavy_loop, (void*)&first_elem); | |
| 41 | pthread_create(&thread_2, NULL, heavy_loop, (void*)&good_elem); | |
| 42 | - | pthread_create(&thread_3, NULL, heavy_loop, (void*)&good_elem); |
| 42 | + | |
| 43 | pthread_join(thread_2, NULL); | |
| 44 | - | pthread_join(thread_3, NULL); |
| 44 | + | |
| 45 | ||
| 46 | printf("%d %d %d\n", array[first_elem],array[bad_elem],array[good_elem]);
| |
| 47 | time1 = (tmsEnd1-tmsBegin1)*1000/CLOCKS_PER_SEC; | |
| 48 | time2 = (tmsEnd2-tmsBegin2)*1000/CLOCKS_PER_SEC; | |
| 49 | time3 = (tmsEnd3-tmsBegin3)*1000/CLOCKS_PER_SEC; | |
| 50 | printf("%lld ms\n", time1);
| |
| 51 | printf("%lld ms\n", time2);
| |
| 52 | printf("%lld ms\n", time3);
| |
| 53 | ||
| 54 | return 0; | |
| 55 | } | |
| 56 | - | } |
| 56 | + | |
| 57 |