Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/time.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <pthread.h>
- #define INPUT_NUM 500000000
- struct timeval t1, t2, rate1, rate2;
- void* thread_func(void* parameter){
- unsigned long *p = (unsigned long*)parameter;
- unsigned long rate;
- float elap_ms;
- gettimeofday(&rate1, NULL);
- while(*p < INPUT_NUM){
- gettimeofday(&rate2, NULL);
- elap_ms = (rate2.tv_sec - rate1.tv_sec) *1000 + (rate2.tv_usec - rate1.tv_usec) / 1000;
- rate = 1000 * *p / elap_ms;
- printf("\r%d %% rate: %lu Hz", 100 * *p/INPUT_NUM, rate );
- }
- }
- int main(){
- pthread_t thread_handle;
- unsigned long i;
- double sum = 0;
- //for betty declare variables outside 'for'
- //input generator
- printf("allocating...\n");
- int16_t* input = (int16_t *) malloc(sizeof(int16_t)*INPUT_NUM);
- if (input==NULL) exit(1);
- printf("generating...\n");
- for (i =0; i < INPUT_NUM; i++) {
- input[i]=rand() % 100;
- sum +=input[i];
- }
- printf("Input avg: %f\n",sum/INPUT_NUM);
- //process data
- printf("processing..\n");
- int ret = pthread_create(&thread_handle, 0, thread_func, &i );
- if( ret != 0){
- printf("Thread not created\n");
- return 1;
- }
- ///// main process block /////
- gettimeofday(&t1, NULL);
- for (i = 0; i < INPUT_NUM ; i++) {
- input[i] *= 0.33f;
- }
- gettimeofday(&t2, NULL);
- ///////////////////////////////
- pthread_join(thread_handle, 0);
- printf("\r100 %\n");
- sum = 0;
- for(i=0; i < INPUT_NUM; i++){
- sum+=input[i];
- }
- printf("Output avg: %f\n", sum/INPUT_NUM);
- float elapsed_ms = (t2.tv_sec - t1.tv_sec) *1000 + (t2.tv_usec - t1.tv_usec) / 1000;
- printf("%f ms\n", elapsed_ms);
- int elapsed_s = elapsed_ms / 1000;
- int elapsed_min = elapsed_s / 60;
- elapsed_s -= elapsed_min*60;
- elapsed_ms -= elapsed_s*1000;
- printf("%d:%d:%f\n",elapsed_min, elapsed_s, elapsed_ms);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement