Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // div100 a 1000000
- // div100 t 1000000
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/time.h>
- int div100(long long dividend) {
- const long long divisor = 0x28f5c29;
- return ((divisor * dividend) >> 32) & 0xffffffff;
- }
- //
- unsigned long microseconds() {
- struct timeval tv;
- gettimeofday(&tv,NULL);
- return 1000000 * tv.tv_sec + tv.tv_usec;
- }
- void testCorrect(long long size) {
- int counter = 0;
- for (long long i = 0; i < size; i++) {
- int expected = i / 100;
- int value = div100(i);
- if (expected != value) {
- printf("FAILED: %lld\t/ 100 = %d\t but value is %d\n", i, expected, value);
- counter++;
- }
- }
- if (counter == 0)
- printf("Succuss\n");
- else
- printf("Failed %d times\n", counter);
- }
- void testTime(long long size) {
- unsigned long last, cur;
- last = microseconds();
- for (long long i = 0; i < size; i++) {
- int value = div100(i);
- }
- cur = microseconds();
- printf("Standart division: %ldµs\n", cur - last);
- last = microseconds();
- for (long long i = 0; i < size; i++) {
- int value = i / 100;
- }
- cur = microseconds();
- printf("Bit-magic division: %ldµs\n", cur - last);
- }
- int main(int argc, char **argv) {
- if (*argv[1] == 'a') { // Check results
- long long size = atoll(argv[2]);
- printf("Test correct: %lld\n", size);
- testCorrect(size);
- }
- else if (*argv[1] == 't') { // Check time
- long long size = atoll(argv[2]);
- printf("Test time: %lld\n", size);
- testTime(size);
- }
- else {
- printf("Unknown option: %c", *argv[1]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement