Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8380

C/C++ • Re: Discrepancy between datatypes performance

$
0
0
Changing the way the two loops works make the thread 50% faster! Instead of doing interleave the first loop to the first half and the second loop do the second part. Instead of calculating the index all the time I simply increment the counter.

Code:

daniel@Pi5:~ $ ./loopcheckOne loop time (us): 1455Two loops in interleave  time (us): 1487Two loops in threads  time (us): 1279Two loops half in threads  time (us): 811checking all result :...Results are identical!
N.B. I'm using an array of 200000. This is a lot of memory. If you reduce the size then the thread is less impressive since you need to create the thread.
ex: an array of 1000

Code:

One loop time (us): 6Two loops in interleave  time (us): 6Two loops in threads  time (us): 166Two loops half in threads  time (us): 59checking all result :...Results are identical!
If I use an empty thread function it takes around 115 us to do thread with nothing.
N.B. the half thread is faster because "pthread_t ptid_a,ptid_b" was already set.

Code:

daniel@Pi5:~ $ gcc -lpthread -o emptyThread emptyThread.cdaniel@Pi5:~ $ ./emptyThreadTwo simple empty threads time (us): 115
source code for empty thread

Code:

#include <stdio.h>#include <stdlib.h>#include <stdint.h>#include <time.h>#include <pthread.h>double diff_timespec_us(const struct timespec *time1, const struct timespec *time0) {  return (time1->tv_sec - time0->tv_sec)      + (time1->tv_nsec - time0->tv_nsec) / 1000.0;}void *  simpleThread(void  * arg){}int main(void){      struct timespec start,end;      clock_gettime(CLOCK_MONOTONIC,&start);      pthread_t ptid_a,ptid_b;      pthread_create(&ptid_a,NULL,&simpleThread,NULL);      pthread_create(&ptid_b,NULL,&simpleThread,NULL);      pthread_join(ptid_a,NULL);      pthread_join(ptid_b,NULL);      clock_gettime(CLOCK_MONOTONIC,&end);      printf("Two simple empty threads time (us): %.0f\n", diff_timespec_us(&end,&start));      return 0;}
In conclusion if you do threading you need to thing about the time it takes to the thread to initialize.

Statistics: Posted by danjperron — Tue Oct 08, 2024 10:46 pm



Viewing all articles
Browse latest Browse all 8380

Trending Articles