void bm_end(const char* name) { benchmark_t* benchmark; struct rusage rs; int ret = getrusage(RUSAGE_SELF, &rs); if (ret != 0) { perror("rusage failed"); exit(-1); } benchmark = find_benchmark(name); benchmark->end = rs; struct timeval utime; timeval_subtract(&utime, &benchmark->end.ru_utime, &benchmark->begin.ru_utime); struct timeval stime; timeval_subtract(&stime, &benchmark->end.ru_stime, &benchmark->begin.ru_stime); printf("Benchmark: %s\n", benchmark->name); printf("User time used : %ld.%06ld\n", utime.tv_sec, (long int)utime.tv_usec); printf("System time used: %ld.%06ld\n", stime.tv_sec, (long int)stime.tv_usec); printf("\n"); }
void gt_timer_show_progress_va(GtTimer *t, FILE *fp, const char *desc, va_list ap) { #ifndef _WIN32 char buf[BUFSIZ]; struct timeval elapsed_tv, elapsed_user_tv, elapsed_sys_tv; gt_assert(t && desc); gettimeofday(&t->stop_tv, NULL); gt_xgetrusage(RUSAGE_SELF, &t->stop_ru); timeval_subtract(&elapsed_tv, &t->stop_tv, &t->start_tv); timeval_subtract(&elapsed_user_tv, &t->stop_ru.ru_utime, &t->start_ru.ru_utime); timeval_subtract(&elapsed_sys_tv, &t->stop_ru.ru_stime, &t->start_ru.ru_stime); gt_timer_print_progress_report(t, &elapsed_tv, &elapsed_user_tv, &elapsed_sys_tv, t->statedesc, fp); if (t->statedesc) gt_free(t->statedesc); (void) vsnprintf(buf, BUFSIZ, desc, ap); t->statedesc = gt_cstr_dup(buf); gettimeofday(&t->start_tv, NULL); gt_xgetrusage(RUSAGE_SELF, &t->start_ru); #else /* XXX */ fprintf(stderr, "gt_timer_show_progress_va() not implemented\n"); exit(EXIT_FAILURE); #endif }
/** * @internal * * @param timeout Timeout value given to select, will be reduced by the * difference since start time. * @param tv_start Actual time before select() was called * * @brief Substract time spent waiting in select from a given * max. timeout struct * * This functions is intended for functions which call select() repeatedly * with a given overall timeout. After each select() call the time already * spent in waiting has to be substracted from the timeout. (Note that we don't * use the Linux select(2) feature to return the time not slept in the timeout * struct, because that's not portable.) * * @return * No direct return; modifies timeout value in the struct pointed to by the * second pointer argument as described above. */ void vbi_capture_io_update_timeout (struct timeval * timeout, const struct timeval * tv_start) { struct timeval delta; struct timeval tv_stop; int errno_saved; errno_saved = errno; gettimeofday(&tv_stop, NULL); errno = errno_saved; /* first calculate difference between start and current time */ timeval_subtract (&delta, &tv_stop, tv_start); if ((delta.tv_sec | delta.tv_usec) < 0) { delta.tv_sec = 0; delta.tv_usec = 0; } else { /* substract delta from the given max. timeout */ timeval_subtract (timeout, timeout, &delta); /* check if timeout was underrun -> set rest timeout to zero */ if ((timeout->tv_sec | timeout->tv_usec) < 0) { timeout->tv_sec = 0; timeout->tv_usec = 0; } } }
void print_resource_track(const char *desc, struct resource_track *track, io_channel channel) { #ifdef HAVE_GETRUSAGE struct rusage r; #endif #ifdef HAVE_MALLINFO struct mallinfo malloc_info; #endif struct timeval time_end; gettimeofday(&time_end, 0); if (desc) printf("%s: ", desc); #ifdef HAVE_MALLINFO #define kbytes(x) (((x) + 1023) / 1024) malloc_info = mallinfo(); printf(_("Memory used: %dk/%dk (%dk/%dk), "), kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd), kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks)); #else printf(_("Memory used: %d, "), (int) (((char *) sbrk(0)) - ((char *) track->brk_start))); #endif #ifdef HAVE_GETRUSAGE getrusage(RUSAGE_SELF, &r); printf(_("time: %5.2f/%5.2f/%5.2f\n"), timeval_subtract(&time_end, &track->time_start), timeval_subtract(&r.ru_utime, &track->user_start), timeval_subtract(&r.ru_stime, &track->system_start)); #else printf(_("elapsed time: %6.3f\n"), timeval_subtract(&time_end, &track->time_start)); #endif #define mbytes(x) (((x) + 1048575) / 1048576) if (channel && channel->manager && channel->manager->get_stats) { io_stats delta = 0; unsigned long long bytes_read = 0; unsigned long long bytes_written = 0; if (desc) printf("%s: ", desc); channel->manager->get_stats(channel, &delta); if (delta) { bytes_read = delta->bytes_read - track->bytes_read; bytes_written = delta->bytes_written - track->bytes_written; } printf("I/O read: %lluMB, write: %lluMB, rate: %.2fMB/s\n", mbytes(bytes_read), mbytes(bytes_written), (double)mbytes(bytes_read + bytes_written) / timeval_subtract(&time_end, &track->time_start)); } }
void periodic_check(int sock, short event, void *arg) { //Set next periodic check event event_add(&update_check_event, &update_check_interval); //Still waiting for the first value to be delivered if(!monitor_initialized) { return; } //Get (2 copies) of current time struct timeval current_time, elapsed_time; gettimeofday(¤t_time, NULL); elapsed_time.tv_sec = current_time.tv_sec; elapsed_time.tv_usec = current_time.tv_usec; //How much time from sample start? timeval_subtract(&elapsed_time, &sample_start); //Time to print, update and reset if(elapsed_time.tv_sec >= UPDATE_INTERVAL || arg != NULL) { double secs, vps, kbps; //Calculate throughput for last sample secs = (((float)elapsed_time.tv_usec)/100000)+elapsed_time.tv_sec; vps = ((float)last_sample_delivered/secs); kbps = ((float)last_sample_bytes/secs)/1000; //Print stats for last sample printf("Last sample: %lu values (%lu bytes) in %.1f secs\n", last_sample_delivered, last_sample_bytes, secs); printf("TP: %.2f v/s, %.2f kb/s\n", vps, kbps); //Update total counter total_delivered += last_sample_delivered; total_bytes += last_sample_bytes; //How much time from delivery of first value? timeval_subtract(¤t_time, &monitor_start); //Calculate throughput overall secs = (((float)current_time.tv_usec)/100000)+current_time.tv_sec; vps = ((float)total_delivered/secs); kbps = ((float)total_bytes/secs)/1000; //Print stats overall printf("Total: %lu values (%lu bytes) in %.1f secs\n", total_delivered, total_bytes, secs); printf("TP: %.2f v/s, %.2f kb/s\n", vps, kbps); fflush(stdout); //Reset last sample stats and timer last_sample_delivered = 0; last_sample_bytes = 0; gettimeofday(&sample_start, NULL); } //Makes the compiler happy sock = sock; event = event; arg = arg; }
int sleep_period(sleep_handler_t *sleep_handler, double *freq) { handler_t *handler=(handler_t *)sleep_handler; struct timeval current_time; struct timeval diff; struct timeval sleep_time_tv; struct timespec sleep_time_ts; // lets take current time if(gettimeofday(¤t_time, NULL)) { free(handler->errstr); x_asprintf(&handler->errstr, "Error calling gettimeofday(): %s\n", strerror_r(errno, handler->errbuf, MAX_ERROR_MSG_LENGTH)); return 2; } // lets calculate diff if(timeval_subtract(&diff, ¤t_time, &handler->last_call_time)) { free(handler->errstr); x_asprintf(&handler->errstr, "Your system clock is running backwards!\n"); handler->last_call_time.tv_sec=current_time.tv_sec; handler->last_call_time.tv_usec=current_time.tv_usec; return 2; } // and sleep time if(timeval_subtract(&sleep_time_tv, &handler->period, &diff)) { double period; // negative, we've got a delay; lets calculate it period=(double)diff.tv_sec; period+=((double)diff.tv_usec)/1e6; *freq=1/period; if(gettimeofday(&handler->last_call_time, NULL)) { free(handler->errstr); x_asprintf(&handler->errstr, "Error calling gettimeofday(): %s\n", strerror_r(errno, handler->errbuf, MAX_ERROR_MSG_LENGTH)); free(handler); return 2; } return 1; } else { // here, just sleep sleep_time_ts.tv_sec=sleep_time_tv.tv_sec; sleep_time_ts.tv_nsec=(long)(sleep_time_tv.tv_usec*1e3); errno=0; if(nanosleep(&sleep_time_ts, NULL)) { free(handler->errstr); x_asprintf(&handler->errstr, "Error calling nanosleep(): %s\n", strerror_r(errno, handler->errbuf, MAX_ERROR_MSG_LENGTH)); } if(gettimeofday(&handler->last_call_time, NULL)) { free(handler->errstr); x_asprintf(&handler->errstr, "Error calling gettimeofday(): %s\n", strerror_r(errno, handler->errbuf, MAX_ERROR_MSG_LENGTH)); free(handler); return 2; } } return 0; }
/** * thread which regularly scans hashtable for expired buckets/flows */ void BaseAggregator::exporterThread() { struct timeval inttimer; gettimeofday(&inttimer, 0); //struct timeval difftime; //REQUIRE(timeval_subtract(&difftime, &stoptime, &starttime) == 0); /*timespec req; req.tv_sec = pollInterval / 1000; req.tv_nsec = (pollInterval % 1000) * 1000;*/ registerCurrentThread(); msg(MSG_INFO, "Polling aggregator each %u msec", pollInterval); while (!exitFlag) { addToCurTime(&inttimer, pollInterval); struct timeval curtime; gettimeofday(&curtime, 0); struct timeval difftime; if (timeval_subtract(&difftime, &inttimer, &curtime)!=1) { // restart nanosleep with the remaining sleep time // if we got interrupted by a signal struct timespec ts; TIMEVAL_TO_TIMESPEC(&difftime, &ts); while (nanosleep(&ts, &ts) == -1 && errno == EINTR); } gettimeofday(&curtime, 0); msg(MSG_VDEBUG,"Aggregator: starting Export"); for (size_t i = 0; i < rules->count; i++) { rules->rule[i]->hashtable->expireFlows(); } struct timeval endtime; gettimeofday(&endtime, 0); timeval_subtract(&difftime, &endtime, &curtime); msg (MSG_VDEBUG,"Aggregator: export took %.03f secs", (float)difftime.tv_usec/1000000+difftime.tv_sec); } if (getShutdownProperly()) { for (size_t i = 0; i < rules->count; i++) { rules->rule[i]->hashtable->expireFlows(true); } } unregisterCurrentThread(); }
static void print_resource_track(struct resource_track *track) { struct rusage r; struct timeval time_end; gettimeofday(&time_end, 0); getrusage(RUSAGE_SELF, &r); printf(_("Memory used: %d, elapsed time: %6.3f/%6.3f/%6.3f\n"), (int) (((char *) sbrk(0)) - ((char *) track->brk_start)), timeval_subtract(&time_end, &track->time_start), timeval_subtract(&r.ru_utime, &track->user_start), timeval_subtract(&r.ru_stime, &track->system_start)); }
void test_local_bus__measure_write_and_then_read_data_rate(unsigned short int should_do_write_test, unsigned short int should_do_read_test) { unsigned long int number_of_writes = 0, number_of_reads = 0, number_of_errors = 0, number_of_loops = 0; unsigned short int value; srand(time(NULL)); // clock_t start_writing, done_writing, start_reading, done_reading; unsigned long int i, j; if (1) { for (i=0; i<BIG_ARRAY_SIZE; i++) { array[i] = pseudo_random(0x0, 0xff); } if (should_do_write_test) { // start_writing = clock(); gettimeofday(&start1, NULL); for (i=0; i<BIG_ARRAY_SIZE; i++) { j = pseudo_random(ARRAY_PARTIAL_RANGE__FIRST, ARRAY_PARTIAL_RANGE__LAST); FINESSE_CSR_WR(slot, j, array[i]); number_of_writes++; } // done_writing = clock(); gettimeofday(&end1, NULL); timeval_subtract(&result1, &end1, &start1); // float seconds_for_write = ((float) (done_writing - start_writing)) / CLOCKS_PER_SEC; float seconds_for_write = result1.tv_sec + result1.tv_usec / 1000000.0; if (seconds_for_write) { float kilobytes_per_second_write = 1.0e-3 * number_of_writes / seconds_for_write; printf("%d bytes in %4.1f seconds = %4.0f kbytes/s (write)\n", number_of_writes, seconds_for_write, kilobytes_per_second_write); } number_of_writes = 0; } if (should_do_read_test) { // start_reading = clock(); gettimeofday(&start2, NULL); for (i=0; i<BIG_ARRAY_SIZE; i++) { j = pseudo_random(ARRAY_PARTIAL_RANGE__FIRST, ARRAY_PARTIAL_RANGE__LAST); FINESSE_CSR(slot, j); number_of_reads++; } // done_reading = clock(); gettimeofday(&end2, NULL); timeval_subtract(&result2, &end2, &start2); // float seconds_for_read = ((float) (done_reading - start_reading)) / CLOCKS_PER_SEC; float seconds_for_read = result2.tv_sec + result2.tv_usec / 1000000.0; if (seconds_for_read) { float kilobytes_per_second_read = 1.0e-3 * number_of_reads / seconds_for_read; printf("%d bytes in %4.1f seconds = %4.0f kbytes/s (read)\n", number_of_reads, seconds_for_read, kilobytes_per_second_read); } number_of_reads = 0; } } }
int main(int argc, char *argv[]){ int m = 5000; int n = 5000; double **data, **fuzzed; struct timeval data_start, data_end, calc_start, calc_end, data_diff, calc_diff; gettimeofday(&data_start, NULL); data = malloc(m * sizeof(double *)); for (int i = 0; i < m; i++){ data[i] = malloc(n * sizeof(double)); } fuzzed = malloc(m * sizeof(double *)); for (int i = 0; i < m; i++){ fuzzed[i] = malloc(n * sizeof(double)); } printf("Generating sample data (%dx%d array), in C...\n", m, n); for (int j = 0; j < m; j++){ for (int k = 0; k < n; k++) { data[j][k] = pow(((float)j/10.0), 4) + pow(((float)k/20.0), 4); } } gettimeofday(&data_end, NULL); printf("Performing gradient calculation on data (%dx%d array), with results:\n", m, n); gettimeofday(&calc_start, NULL); del2(data, fuzzed, m, n); gettimeofday(&calc_end, NULL); for (int i = 0; i< min(m, n); i+=1000){ printf("%f\n", fuzzed[i][i]); } timeval_subtract(&data_diff, &data_end, &data_start); timeval_subtract(&calc_diff, &calc_end, &calc_start); printf("Summary:\n"); printf(" Data generation code segment (C) took %f seconds\n", data_diff.tv_sec + data_diff.tv_usec/1e6); printf(" Gradient calculation code segment (C) took %f seconds\n", calc_diff.tv_sec + calc_diff.tv_usec/1e6); printf(" Total time: %.3f seconds\n", (data_diff.tv_sec + data_diff.tv_usec/1e6) + (calc_diff.tv_sec + calc_diff.tv_usec/1e6)); // We should free() data, and fuzzed here. I'm lazy though. return 0; }
// Multipairing void do_multi(int m){ int i = 0; lpoly list[m]; lpoly *tmp_list; // prevalues element_t gg[m]; element_ptr ggg[m]; element_t hh[m]; for(i = 0; i < m; i++){ element_init_G2(gg[i], pairing); element_random(gg[i]); ggg[i] = malloc(sizeof(element_ptr)); element_init(ggg[i], gg[i]->field); element_set(ggg[i], gg[i]); element_init_G1(hh[i], pairing); element_random(hh[i]); } // precomputation gettimeofday(&tvBegin, NULL); for(i = 0; i < m; i++){ tmp_list = lpoly_init(); precompute(tmp_list, pairing->r, hh[i], gg[i]); list[i] = *tmp_list; } gettimeofday(&tvEnd, NULL); timeval_subtract(&tvEnd, &tvBegin, 1000); // compute gettimeofday(&tvBegin, NULL); compute_millers(temp2, list, ggg, m, pairing); gettimeofday(&tvEnd, NULL); timeval_subtract(&tvEnd, &tvBegin, 1000); gettimeofday(&tvBegin, NULL); element_prod_pairing(temp1, hh, gg, m); gettimeofday(&tvEnd, NULL); timeval_subtract(&tvEnd, &tvBegin, 1000); for(i = 0; i < m; i++){ lpoly_free2(list[i]); } }
int do_test(char * name, int (* test)(unsigned int len), unsigned int len, int count) { int i, ret; struct timeval tv_in; struct timeval tv_beg; struct timeval tv_end; struct timeval tv_len; struct timeval tv_lat; struct timezone tz; char * status = "OK"; char * latency_type = ""; tv_in.tv_sec = len / 1000; tv_in.tv_usec = (len % 1000) * 1000; gettimeofday(&tv_beg, &tz); for (i = 0; i < count; i++) { ret = test(len); } gettimeofday(&tv_end, &tz); ret = timeval_subtract(&tv_len, &tv_end, &tv_beg); if (ret) status = "ERROR"; ret = timeval_subtract(&tv_lat, &tv_len, &tv_in); if (ret) { latency_type = "-"; timeval_subtract(&tv_lat, &tv_in, &tv_len); } if (tv_lat.tv_sec > 0 || tv_lat.tv_usec > MAX_LATENCY) status = "ERROR"; printf(" Test: %6s %4ums time: %2u.%06us " "latency: %1s%u.%06us status: %s\n", name, (len * count), (unsigned int)tv_len.tv_sec, (unsigned int)tv_len.tv_usec, latency_type, (unsigned int)tv_lat.tv_sec, (unsigned int)tv_lat.tv_usec, status); return ret; }
int main() { unsigned int OUTER_LOOP_COUNT, NUM_X, NUM_Y, NUM_T; const REAL s0 = 0.03, strike = 0.03, t = 5.0, alpha = 0.2, nu = 0.6, beta = 0.5; readDataSet( OUTER_LOOP_COUNT, NUM_X, NUM_Y, NUM_T ); const int Ps = get_CPU_num_threads(); REAL* res = (REAL*)malloc(OUTER_LOOP_COUNT*sizeof(REAL)); { // Original Program (Sequential CPU Execution) cout<<"\n// Running OpenMP parallelized project program"<<endl; unsigned long int elapsed = 0; struct timeval t_start, t_end, t_diff; gettimeofday(&t_start, NULL); run_OrigCPU( OUTER_LOOP_COUNT, NUM_X, NUM_Y, NUM_T, s0, t, alpha, nu, beta, res ); gettimeofday(&t_end, NULL); timeval_subtract(&t_diff, &t_end, &t_start); elapsed = t_diff.tv_sec*1e6+t_diff.tv_usec; // validation and writeback of the result bool is_valid = validate ( res, OUTER_LOOP_COUNT ); writeStatsAndResult( is_valid, res, OUTER_LOOP_COUNT, NUM_X, NUM_Y, NUM_T, false, Ps, elapsed ); } return 0; }
void toc(struct timeval *timer) { struct timeval tv_end, tv_diff; gettimeofday(&tv_end, NULL); timeval_subtract(&tv_diff, &tv_end, timer); printf("cpuTime = %ld.%06ld (s)\n", tv_diff.tv_sec, tv_diff.tv_usec); }
void cable_parallel_phys_wait() { /* This busy wait attempts to make the frequency exactly 200kHz, * including the processing time between ticks. * This means a period of 5us, or half a period of 2.5us. */ #ifdef PARALLEL_USE_PROCESS_TIMER struct timespec ts; do { clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); } while((ts.tv_sec == 0) && (ts.tv_nsec < 2500)); /* Doing the set after the check means that processing time * is not added to the wait. */ ts.tv_sec = 0; ts.tv_nsec = 0; clock_settime(CLOCK_PROCESS_CPUTIME_ID, &ts); #else struct timeval now_tv; struct timeval results_tv; do { gettimeofday(&now_tv, NULL); timeval_subtract (&results_tv, &now_tv, &last_tv); } while((results_tv.tv_sec == 0) && (results_tv.tv_usec < 3)); last_tv = now_tv; #endif }
/* Function to apply the filter with the filter list in the outside loop */ void serialDataFirst ( int data_len, unsigned int* input_array, unsigned int* output_array, int filter_len, unsigned int* filter_list ) { /* Variables for timing */ struct timeval ta, tb, tresult; /* get initial time */ gettimeofday ( &ta, NULL ); /* for all elements in the data */ for (int x=0; x<data_len; x++) { /* for all elements in the filter */ for (int y=0; y<filter_len; y++) { /* it the data element matches the filter */ if (input_array[x] == filter_list[y]) { /* include it in the output */ output_array[x] = input_array[x]; } } } /* get initial time */ gettimeofday ( &tb, NULL ); timeval_subtract ( &tresult, &tb, &ta ); printf ("Serial data first took %lu seconds and %lu microseconds. Filter length = %d\n", tresult.tv_sec, tresult.tv_usec, filter_len ); }
int main() { pj_sock_t sock; pj_sockaddr_in to_addr; char *s = "Cong hoa xa hoi chu nghia VietNam"; char buffer[100]; pj_ssize_t len; struct timeval tv_begin, tv_end, tv_diff; pj_log_set_level(3); CHECK(__FILE__, pj_init()); pj_bzero(buffer, sizeof(buffer)); CHECK_R( __FILE__, pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock) ); //udp_socket(12345, &sock); setup_addr_with_host_and_port(&to_addr, "127.0.0.1", 33333); len = strlen(s); gettimeofday(&tv_begin, NULL); pj_sock_sendto(sock, s, &len, 0, &to_addr, sizeof(to_addr)); PJ_LOG(3, (__FILE__, "Sent: %s", s)); len = 100; pj_sock_recv(sock, buffer, &len, 0); gettimeofday(&tv_end, NULL); int diff = timeval_subtract(&tv_diff, &tv_end, &tv_begin); PJ_LOG(3, (__FILE__, "Received: %s %0.2f msec", buffer, diff*1.0/1000)); pj_shutdown(); return 0; }
off_t rateest_frames_played(void) { GTimeVal tv,tv2; int i; gfloat f,sr; off_t o,lat; /* First, use clock time, checkpoint time and estimated sample * rate to calculate position. * If the clock seems broken, use written frames and estimated latency. */ g_get_current_time(&tv); i = timeval_subtract(&tv2,&tv,¤t_checkpoint_time); sr = rateest_real_samplerate(); if (i >= 0) { /* printf("tv2: %ld.%6ld\n",tv2.tv_sec,tv2.tv_usec); */ f = (gfloat)tv2.tv_usec * 0.000001 + (gfloat)tv2.tv_sec; f *= sr; /* printf("f: %f, sr: %f\n",f,sr); */ o = current_checkpoint + (off_t)f; /* printf("First guess: %d, total_written: %d\n",(int)o, (int)total_written_frames); */ if (o>total_written_frames) o=total_written_frames; } if (i<0) { if (best_seconds > current_played_seconds) lat = best_written - (best_seconds + best_discarded_seconds)*sr; else lat = (total_written_frames - current_checkpoint) - (current_played_seconds + current_discarded_seconds)*sr; o = total_written_frames - lat; } if (o > last_frames_played_value) last_frames_played_value = o; return last_frames_played_value; }
int main(int argc, char *argv[]){ int i = 0; time_t curtime; struct timeval tvBegin; struct timeval tvEnd; struct timeval tvDiff; gettimeofday(&tvBegin, NULL); if(argc < 2){ printf("Default to 50%% CPU\n"); while(1){ while(1){ gettimeofday(&tvEnd, NULL); timeval_subtract(&tvDiff, &tvEnd, &tvBegin); //printf("%ld, %ld\n", tvDiff.tv_sec,tvDiff.tv_usec); if(tvDiff.tv_usec > 11000){ break; } } usleep(10000); gettimeofday(&tvBegin, NULL); } } //printf("%s\n", argv[1]); return 0; }
int main(int argc, char *argv[]) { int n, /* loop variables */ pc, /* prime counter */ foundone; /* most recent prime found */ struct timeval t_begin, t_end, t_diff, t_total; //printf("Starting. Numbers to be scanned= %d\n",LIMIT); gettimeofday(&t_begin, NULL); pc=4; /* Assume the primes less than 10 (2,3,5,7) are counted here */ for (n=11; n<=LIMIT; n=n+2) { if (isprime(n)) { pc++; foundone = n; /***** Optional: print each prime as it is found printf("%d\n",foundone); *****/ } //if ( (n-1)%PRINT == 0 ) // printf("Numbers scanned= %d Primes found= %d\n",n-1,pc); } gettimeofday(&t_end, NULL); timeval_subtract(&t_diff, &t_end, &t_begin); //printf("Done. Largest prime is %d Total primes %d\n",foundone,pc); printf("%d %ld.%06ld\n",LIMIT,t_diff.tv_sec, t_diff.tv_usec); }
void buffered_string_benchmark(void){ //basic test to see if BufStringAdd is working at all, should print out <123456abcdefg> BufString *a = BufStringNewSize(10/*initial size*/); BufStringAdd(a, "123456"); BufStringAdd(a, "abcdefg"); DEBUG("Test <%s>\n", a->bs_Buffer); for (unsigned int i = 0; i < 25; i++){ struct timeval start_time, end_time; gettimeofday(&start_time, NULL); BufString *b = BufStringNewSize(10/*initial size*/); for (unsigned int j = 0; j < 100000000; j++){ BufStringAdd(b, "123456790"); } // DEBUG("Total buffer size %d", b->bs_Size); gettimeofday(&end_time, NULL); struct timeval duration; timeval_subtract(&duration, &end_time, &start_time); DEBUG("Iteration %d execution time %ld.%04lds\n", i, duration.tv_sec, duration.tv_usec/1000); BufStringDelete(b); } exit(1); }
uint32_t process_ett_stop_time(uint8_t neighbor_main_addr[ETH_ALEN], struct timeval* ett_stop_time) { olsr_db_neighbors_ett_entry_t* entry; if(neighbor_set_ett == NULL) { return false; } HASH_FIND(hh, neighbor_set_ett, neighbor_main_addr, ETH_ALEN, entry); if(entry == NULL) { return false; } // If no previous ETT-START package was received false is returned if(entry->timeval_recv.tv_sec == 0 && entry->timeval_recv.tv_usec == 0) { return false; } // calculates the difference between ett_stop_time and timeval_recv struct timeval res; timeval_subtract(&res, &(entry->timeval_recv), ett_stop_time); // calculates the difference time in usec uint32_t result = res.tv_sec * 1000000 + res.tv_usec; // deleting the old timeval_recv value entry->timeval_recv.tv_sec = 0; entry->timeval_recv.tv_usec = 0; return result; }
// IPC Execute enum esif_rc ipc_execute (struct esif_ipc *ipc) { enum esif_rc rc = ESIF_OK; struct timeval start = {0}; struct timeval finish = {0}; struct timeval result; if (g_ipc_handle == ESIF_INVALID_HANDLE) { rc = ESIF_E_NO_LOWER_FRAMEWORK; goto exit; } if (g_timestamp) { esif_ccb_get_time(&start); } rc = esif_ipc_execute(g_ipc_handle, ipc); if (g_timestamp) { esif_ccb_get_time(&finish); } if (g_timestamp) { ESIF_TRACE_DEBUG("Start time: %06lu.%06lu\n", start.tv_sec, start.tv_usec); ESIF_TRACE_DEBUG("Finish time: %06lu.%06lu\n", finish.tv_sec, finish.tv_usec); timeval_subtract(&result, &finish, &start); ESIF_TRACE_DEBUG("IPC Exec Time: %06lu.%06lu (%06lu usecs)\n", result.tv_sec, result.tv_usec, result.tv_usec); } exit: return rc; }
int main(int argc, char *argv[]) { // clock_t begin, end; // double time_spent; // begin = clock(); // end = clock(); // time_spent = (double)(end - begin) / CLOCKS_PER_SEC; // fprintf( stderr,"%f\n", time_spent); // return 0; struct timeval tvBegin, tvEnd, tvDiff; // begin gettimeofday(&tvBegin, NULL); // lengthy operation system(argv[1]); //end gettimeofday(&tvEnd, NULL); // diff timeval_subtract(&tvDiff, &tvEnd, &tvBegin); fprintf(stderr ,"%ld.%06ld\n", (long)tvDiff.tv_sec, (long)tvDiff.tv_usec); return 0; }
int main(void) { int *values = malloc(NUMVALUES * sizeof (*values)); unsigned long idx, sortnum; struct timeval start, end, diff, tmp, runtime = {0,0}; assert(values != NULL); for (sortnum = 0; sortnum < NUMSORTS; ++sortnum) { for (idx = 0; idx < NUMVALUES; ++idx) { values[idx] = rand(); } (void) gettimeofday(&start, NULL); //qsort(values, sizeof(*values), NUMVALUES, compare); qsort(values, NUMVALUES, sizeof(*values), compare); (void) gettimeofday(&end, NULL); (void) timeval_subtract(&diff, &end, &start); tmp = runtime; (void) timeval_add(&runtime, &tmp, &diff); /* printf("Runtime (hh:mm:ss:us)\t%02lu:%02lu:%02lu:%06lu\n", runtime.tv_sec / 60LU / 60LU, (runtime.tv_sec / 60LU) % 60LU, runtime.tv_sec % 60LU, runtime.tv_usec); */ } printf("Runtime (hh:mm:ss:us)\t%02lu:%02lu:%02lu:%06lu\n", runtime.tv_sec / 60LU / 60LU, (runtime.tv_sec / 60LU) % 60LU, runtime.tv_sec % 60LU, runtime.tv_usec); free(values); return 0; }
void Timer::elapsed(int64_t *elapsed_cpu, int64_t *elapsed_wall) { if (stopped_elapsed_cpu >= 0 && stopped_elapsed_wall >= 0) { /// Timer was previously stopped with stop() if (elapsed_cpu != nullptr) *elapsed_cpu = stopped_elapsed_cpu; if (elapsed_wall != nullptr) *elapsed_wall = stopped_elapsed_wall; return; } if (elapsed_cpu != nullptr) { struct timespec now_cpu; if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &now_cpu) == 0) { const int64_t now_us = (int64_t)now_cpu.tv_sec * 1000000 + now_cpu.tv_nsec / 1000; const int64_t previous_us = (int64_t)previous_cpu.tv_sec * 1000000 + previous_cpu.tv_nsec / 1000; *elapsed_cpu = now_us - previous_us; } else *elapsed_cpu = -1; } if (elapsed_wall != nullptr) { struct timeval now_wall, result_wall; if (gettimeofday(&now_wall, NULL) == 0 && timeval_subtract(&result_wall, &now_wall, &previous_wall) == 0) { *elapsed_wall = (int64_t)result_wall.tv_sec * 1000000 + result_wall.tv_usec; } else *elapsed_wall = -1; } }
/** print uptime stats */ static int print_uptime(SSL* ssl, struct worker* worker) { struct timeval now = *worker->env.now_tv; struct timeval up, dt; timeval_subtract(&up, &now, &worker->daemon->time_boot); timeval_subtract(&dt, &now, &worker->daemon->time_last_stat); worker->daemon->time_last_stat = now; if(!ssl_printf(ssl, "time.now"SQ"%d.%6.6d\n", (unsigned)now.tv_sec, (unsigned)now.tv_usec)) return 0; if(!ssl_printf(ssl, "time.up"SQ"%d.%6.6d\n", (unsigned)up.tv_sec, (unsigned)up.tv_usec)) return 0; if(!ssl_printf(ssl, "time.elapsed"SQ"%d.%6.6d\n", (unsigned)dt.tv_sec, (unsigned)dt.tv_usec)) return 0; return 1; }
int main(int argc, char * argv[]) { if ( argc == 1 ) { printf("Usage: %s hostname [port]\n", argv[0]); return 1; } char * host = argv[1]; int port = (argc==2) ? DEFAULT_PORT : atoi(argv[2]); // resolving the hostname struct hostent * he; extern h_errno; he = gethostbyname(host); if ( he == NULL ) { fprintf(stderr, "tcpping: unknown host %s (error %d)\n", host, h_errno); return 1; } // filling up `sockaddr_in` structure memset(&addrServer, 0, sizeof(struct sockaddr_in)); addrServer.sin_family = AF_INET; memcpy(&(addrServer.sin_addr.s_addr), he->h_addr, he->h_length); addrServer.sin_port = htons(port); // first IP address as the target struct in_addr ** addr_list = (struct in_addr **) he->h_addr_list; char ip[16]; strcpy(ip, inet_ntoa(*addr_list[0])); // Ctrl+C handler signal(SIGINT, intHandler); // note the starting time struct timeval tvBegin, tvEnd, tvDiff; gettimeofday(&tvBegin, NULL); // main loop while (running) ping(ip, port); // note the ending time and calculate the duration of TCP ping gettimeofday(&tvEnd, NULL); long int diff = timeval_subtract(&tvEnd, &tvBegin); // summary printf ("\n--- %s tcpping statistics ---\n", host); printf ("%d packets transmitted, %d received, %d%% packet loss, time %ldms\n", seq, cnt_successful, 100-100*cnt_successful/seq, diff/1000); if ( cnt_successful > 0 ) { diffAvg = diffSum/cnt_successful; diffMdev = sqrt( diffSum2/cnt_successful - diffAvg*diffAvg ); printf ("rtt min/avg/max/mdev = %0.3lf/%0.3lf/%0.3lf/%0.3lf ms\n", diffMin/1000.,diffAvg/1000.,diffMax/1000.,diffMdev/1000.); } return 0; }
int main(int argc, char** argv){ int i, status; pid_t forkId; pid_t * processId; init(argc,argv); //int j; //for(j=0; j < 10; j++){ //get time here struct timeval tvBegin, tvEnd, tvDiff; processId = (pid_t *)malloc(sizeof(pid_t) * (numUnixProcess - 1)); if(NULL == processId){ printf("error"); exit(1); } gettimeofday(&tvBegin, NULL); printf("starting...\n"); //////////////////////////////////////////////////////////////////// for(i = 1; i < numUnixProcess; i++){ forkId = fork(); if(forkId != 0){ //processo pai UnixProcessFunction(0); }else{ //processo forked UnixProcessFunction(i); exit(0); } //printf("\nProcesso %d\n", forkId); processId[i-1] = forkId; } waitpid(forkId, &status, 0); //////////////////////////////////////////////////////////////////// //get time here gettimeofday(&tvEnd, NULL); printf("finish\n"); //print time diff timeval_subtract(&tvDiff, &tvEnd, &tvBegin); printf("\n\tTime elapsed = %ld.%06ld\n", tvDiff.tv_sec, tvDiff.tv_usec); //} writeMatrix("out.txt",matrixResult,mResultlines,mResultcolumns); return 1; }
int duration_measure(struct duration *duration) { struct timeval end; int retval = gettimeofday(&end, NULL); if (0 == retval) timeval_subtract(&duration->elapsed, &end, &duration->start); return retval; }