void test_const_n() { int n = 15; int c[15] = {100,200,300,400,500,600,700,800,900,1000,1100,1200,1300,1400,1500}; std::ofstream res("res2.txt"); res << "0 0 0" << std::endl; for(int i=0; i<15; i++) { double a=0, b=0; timespec begin, end; for(int j=0; j<TESTS; j++) { knapsack_data d = generate(n); printf("TEST c=%d #%d\n",c[i],j); //dynamic clock_gettime(CLOCK_REALTIME, &begin); knapsack_dynamic(n, c[i], d.value, d.weight); clock_gettime(CLOCK_REALTIME, &end); a+=timespec_to_seconds(&begin, &end); printf("dynamic done\n"); //brute force clock_gettime(CLOCK_REALTIME, &begin); knapsack_bf_iface(n, c[i], d.value, d.weight); clock_gettime(CLOCK_REALTIME, &end); b+=timespec_to_seconds(&begin, &end); printf("brute force done\n\n\n"); free(d.weight); free(d.value); } a/=TESTS; b/=TESTS; res << c[i] << " " << a << " " << b << std::endl; } res.close(); return; }
/* set the timer */ static int create_alarm(struct timespec_t *ts) { struct itimerspec wakeup; struct sigevent sigev; union sigval sigv; timer_t timerid; memset(&wakeup, 0, sizeof(struct itimerspec)); /* if we have a command that shall be exexuted, hook it up to the signal event */ if(event.cmd) { memset(&sigev, 0, sizeof(struct sigevent)); sigv.sival_ptr = (void *)&event; sigev.sigev_notify = SIGEV_THREAD; sigev.sigev_notify_function = signal_event; sigev.sigev_value = sigv; } /* init timer */ if(timer_create(CLOCK_REALTIME_ALARM, event.cmd ? &sigev : NULL, &timerid) != 0) { perror("error: failed to create timer"); return 1; } /* init itimerspec */ if(clock_gettime(CLOCK_REALTIME_ALARM, &wakeup.it_value)) { perror("error: failed to get time from RTC"); return 1; } /* set itimerspec to some future time */ wakeup.it_value.tv_sec += timespec_to_seconds(ts); if(timer_settime(timerid, TIMER_ABSTIME, &wakeup, NULL)) { perror("error: failed to set wakeup time"); return 1; } /* informational message */ fprintf(stdout, "timer set for wakeup in: %ld hours %ld min %ld sec\n", ts->hour, ts->min, ts->sec); /* tick, tock makes the clock */ fprintf(stdout, "tick.\n"); if(event.cmd == NULL) { fprintf(stdout, "tock.\n"); } return 0; }
static int parse_timespec(int optind, int argc, char **argv, struct timespec_t *ts) { if(epochtime) { char *endptr; int64_t now, epoch, diff; epoch = strtoll(argv[optind], &endptr, 10); if(*endptr != '\0' || errno == ERANGE) { fprintf(stderr, "failed to parse absolute time: %s: %s", argv[optind], strerror(errno == ERANGE ? ERANGE : EINVAL)); return 1; } /* get the current time */ if(time((time_t *)&now) == (time_t)-1) { fprintf(stderr, "error: failed to get current time\n"); return 1; } if(epoch < now) { fprintf(stderr, "error: cannot specify a time in the past\n"); return 1; } diff = epoch - now; ts->hour = diff / 3600; diff %= 3600; ts->min = diff / 60; ts->sec = diff % 60; } else { while(optind < argc) { if(parse_timefragment(argv[optind], ts) != 0) { fprintf(stderr, "error: failed to parse time: %s\n", argv[optind]); return 1; } optind++; } if(timespec_to_seconds(ts) == 0) { fprintf(stderr, "error: duration must be non-zero\n"); return 1; } } return 0; }
void turniej(knapsack_data d) { timespec begin, end; //printf("c=%d n=%d\n",d.c, d.n); //for(int i=0; i<d.n; i++) printf("p[%d]=%d w[%d]=%d\n",i,d.value[i],i,d.weight[i]); clock_gettime(CLOCK_REALTIME, &begin); knapsack_bf_iface(d.n,d.c,d.value,d.weight); clock_gettime(CLOCK_REALTIME, &end); printf("Czas działania: %f\n",timespec_to_seconds(&begin, &end)); printf("Max value is: %d\n", best); printf("The solution is: "); //printf("res_x: %lld\n\n",res_x); for(int i=0; i<=d.n; i++) { if(res_x%2) printf("%d ", i+1); res_x=res_x>>1; } printf("\n"); free(d.value); free(d.weight); }