# HG changeset patch # User Chris Cannam # Date 1660053696 -3600 # Tue Aug 09 15:01:36 2022 +0100 # Node ID 321a035f16d6d25674ebc56f4017c417e9f977d7 # Parent 0b12d71e996192a38c5497893a0c78c7666fc7c1 More meaningful timing code diff --git a/test/Timings.cpp b/test/Timings.cpp --- a/test/Timings.cpp +++ b/test/Timings.cpp @@ -1,6 +1,7 @@ /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ #include "bqvec/VectorOpsComplex.h" +#include "bqvec/Allocators.h" #include #include @@ -10,8 +11,6 @@ using namespace std; using namespace breakfastquay; -//!!! This is nonsense. TODO: Replace it with sense. - #ifdef _WIN32 #define drand48() (-1+2*((float)rand())/RAND_MAX) #endif @@ -21,11 +20,12 @@ { cerr << "testVectorOps: testing v_multiply complex" << endl; + const int iterations = 50000; const int N = 1024; - //!!! todo: use aligned allocate(), otherwise results will vary randomly - bq_complex_t target[N]; - bq_complex_t src1[N]; - bq_complex_t src2[N]; + + bq_complex_t *target = allocate(N); + bq_complex_t *src1 = allocate(N); + bq_complex_t *src2 = allocate(N); for (int i = 0; i < N; ++i) { src1[i].re = drand48(); @@ -34,60 +34,55 @@ src2[i].im = drand48(); } - double mean, first, last, total = 0; - for (int i = 0; i < N; ++i) { - bq_complex_t result; - c_multiply(result, src1[i], src2[i]); - if (i == 0) first = result.re; - if (i == N-1) last = result.im; - total += result.re; - total += result.im; - } - mean = total / (N*2); - cerr << "Naive method: mean = " << mean << ", first = " << first - << ", last = " << last << endl; - - v_multiply_to(target, src1, src2, N); - total = 0; - - for (int i = 0; i < N; ++i) { - if (i == 0) first = target[i].re; - if (i == N-1) last = target[i].im; - total += target[i].re; - total += target[i].im; - } - mean = total / (N*2); - cerr << "v_multiply: mean = " << mean << ", first = " << first - << ", last = " << last << endl; - - int iterations = 50000; -// cerr << "Iterations: " << iterations << endl; - -// cerr << "CLOCKS_PER_SEC = " << CLOCKS_PER_SEC << endl; float divisor = float(CLOCKS_PER_SEC) / 1000.f; - clock_t start = clock(); + double first, last, total = 0; for (int j = 0; j < iterations; ++j) { - for (int i = 0; i < N; ++i) { - c_multiply(target[i], src1[i], src2[i]); - } + for (int i = 0; i < N; ++i) { + bq_complex_t result; + c_multiply(result, src1[i], src2[i]); + if (i == 0) first = result.re; + if (i == N-1) last = result.im; + total += result.re; + total += result.im; + total += j; + } } - + clock_t end = clock(); - cerr << "Time for naive method: " << float(end - start)/divisor << endl; + cerr << "repeated c_multiply: first = " << first << ", last = " << last + << ", total = " << total << endl; + cerr << "time for repeated c_multiply: " << float(end - start)/divisor + << endl; start = clock(); + first = last = total = 0; + for (int j = 0; j < iterations; ++j) { v_multiply_to(target, src1, src2, N); + for (int i = 0; i < N; ++i) { + if (i == 0) first = target[i].re; + if (i == N-1) last = target[i].im; + total += target[i].re; + total += target[i].im; + total += j; + } } - + end = clock(); - cerr << "Time for v_multiply: " << float(end - start)/divisor << endl; + cerr << "v_multiply_to: first = " << first << ", last = " << last + << ", total = " << total << endl; + cerr << "time for v_multiply_to: " << float(end - start)/divisor << endl; + deallocate(target); + deallocate(src1); + deallocate(src2); + + cerr << endl; return true; } @@ -96,72 +91,68 @@ { cerr << "testVectorOps: testing v_polar_to_cartesian" << endl; + const int iterations = 50000; const int N = 1024; - bq_complex_t target[N]; - bq_complex_element_t mag[N]; - bq_complex_element_t phase[N]; + + bq_complex_t *target = allocate(N); + bq_complex_element_t *mag = allocate(N); + bq_complex_element_t *phase = allocate(N); for (int i = 0; i < N; ++i) { mag[i] = drand48(); phase[i] = (drand48() * M_PI * 2) - M_PI; } - double mean, first, last, total = 0; - for (int i = 0; i < N; ++i) { - double real = mag[i] * cos(phase[i]); - double imag = mag[i] * sin(phase[i]); - if (i == 0) first = real; - if (i == N-1) last = imag; - total += real; - total += imag; - } - mean = total / (N*2); - cerr << "Naive method: mean = " << mean << ", first = " << first - << ", last = " << last << endl; - - v_polar_to_cartesian(target, mag, phase, N); - - total = 0; - - for (int i = 0; i < N; ++i) { - if (i == 0) first = target[i].re; - if (i == N-1) last = target[i].im; - total += target[i].re; - total += target[i].im; - } - mean = total / (N*2); - cerr << "v_polar_to_cartesian: mean = " << mean << ", first = " << first - << ", last = " << last << endl; - - int iterations = 10000; -// cerr << "Iterations: " << iterations << endl; - -// cerr << "CLOCKS_PER_SEC = " << CLOCKS_PER_SEC << endl; float divisor = float(CLOCKS_PER_SEC) / 1000.f; - clock_t start = clock(); + double first, last, total = 0; for (int j = 0; j < iterations; ++j) { - for (int i = 0; i < N; ++i) { - target[i].re = mag[i] * cos(phase[i]); - target[i].im = mag[i] * sin(phase[i]); - } + for (int i = 0; i < N; ++i) { + double real = mag[i] * cos(phase[i]); + double imag = mag[i] * sin(phase[i]); + if (i == 0) first = real; + if (i == N-1) last = imag; + total += real; + total += imag; + total += j; + } } - + clock_t end = clock(); - cerr << "Time for naive method: " << float(end - start)/divisor << endl; - + cerr << "naive method: first = " << first << ", last = " << last + << ", total = " << total << endl; + cerr << "time for naive method: " << float(end - start)/divisor + << endl; + start = clock(); + first = last = total = 0; + for (int j = 0; j < iterations; ++j) { - v_polar_to_cartesian(target, mag, phase, N); + v_polar_to_cartesian(target, mag, phase, N); + for (int i = 0; i < N; ++i) { + if (i == 0) first = target[i].re; + if (i == N-1) last = target[i].im; + total += target[i].re; + total += target[i].im; + total += j; + } } - + end = clock(); - cerr << "Time for v_polar_to_cartesian: " << float(end - start)/divisor << endl; - + cerr << "v_polar_to_cartesian: first = " << first << ", last = " << last + << ", total = " << total << endl; + cerr << "time for v_polar_to_cartesian: " << float(end - start)/divisor + << endl; + + deallocate(target); + deallocate(mag); + deallocate(phase); + + cerr << endl; return true; } @@ -170,71 +161,66 @@ { cerr << "testVectorOps: testing v_polar_interleaved_to_cartesian" << endl; + const int iterations = 50000; const int N = 1024; - bq_complex_t target[N]; - bq_complex_element_t source[N*2]; + + bq_complex_t *target = allocate(N); + bq_complex_element_t *source = allocate(N*2); for (int i = 0; i < N; ++i) { source[i*2] = drand48(); source[i*2+1] = (drand48() * M_PI * 2) - M_PI; } - double mean, first, last, total = 0; - for (int i = 0; i < N; ++i) { - double real = source[i*2] * cos(source[i*2+1]); - double imag = source[i*2] * sin(source[i*2+1]); - if (i == 0) first = real; - if (i == N-1) last = imag; - total += real; - total += imag; - } - mean = total / (N*2); - cerr << "Naive method: mean = " << mean << ", first = " << first - << ", last = " << last << endl; - - v_polar_interleaved_to_cartesian(target, source, N); - - total = 0; - - for (int i = 0; i < N; ++i) { - if (i == 0) first = target[i].re; - if (i == N-1) last = target[i].im; - total += target[i].re; - total += target[i].im; - } - mean = total / (N*2); - cerr << "v_polar_interleaved_to_cartesian: mean = " << mean << ", first = " << first - << ", last = " << last << endl; - - int iterations = 10000; -// cerr << "Iterations: " << iterations << endl; - -// cerr << "CLOCKS_PER_SEC = " << CLOCKS_PER_SEC << endl; float divisor = float(CLOCKS_PER_SEC) / 1000.f; - clock_t start = clock(); + double first, last, total = 0; for (int j = 0; j < iterations; ++j) { - for (int i = 0; i < N; ++i) { - target[i].re = source[i*2] * cos(source[i*2+1]); - target[i].im = source[i*2] * sin(source[i*2+1]); - } + for (int i = 0; i < N; ++i) { + double real = source[i*2] * cos(source[i*2+1]); + double imag = source[i*2] * sin(source[i*2+1]); + if (i == 0) first = real; + if (i == N-1) last = imag; + total += real; + total += imag; + total += j; + } } - + clock_t end = clock(); - cerr << "Time for naive method: " << float(end - start)/divisor << endl; - + cerr << "naive method: first = " << first << ", last = " << last + << ", total = " << total << endl; + cerr << "time for naive method: " << float(end - start)/divisor + << endl; + start = clock(); + first = last = total = 0; + for (int j = 0; j < iterations; ++j) { - v_polar_interleaved_to_cartesian(target, source, N); + v_polar_interleaved_to_cartesian(target, source, N); + for (int i = 0; i < N; ++i) { + if (i == 0) first = target[i].re; + if (i == N-1) last = target[i].im; + total += target[i].re; + total += target[i].im; + total += j; + } } - + end = clock(); - cerr << "Time for v_polar_interleaved_to_cartesian: " << float(end - start)/divisor << endl; - + cerr << "v_polar_interleaved_to_cartesian: first = " << first << ", last = " << last + << ", total = " << total << endl; + cerr << "time for v_polar_interleaved_to_cartesian: " << float(end - start)/divisor + << endl; + + deallocate(target); + deallocate(source); + + cerr << endl; return true; } @@ -243,77 +229,69 @@ { cerr << "testVectorOps: testing v_cartesian_to_polar" << endl; + const int iterations = 50000; const int N = 1024; - bq_complex_t source[N]; - bq_complex_element_t mag[N]; - bq_complex_element_t phase[N]; + + bq_complex_t *source = allocate(N); + bq_complex_element_t *mag = allocate(N); + bq_complex_element_t *phase = allocate(N); for (int i = 0; i < N; ++i) { source[i].re = (drand48() * 2.0) - 1.0; source[i].im = (drand48() * 2.0) - 1.0; } - double mean, first, last, total = 0; - for (int i = 0; i < N; ++i) { - double mag = sqrt(source[i].re * source[i].re + source[i].im * source[i].im); - double phase = atan2(source[i].im, source[i].re); - if (i == 0) first = mag; - if (i == N-1) last = phase; - total += mag; - total += phase; - } - mean = total / (N*2); - cerr << "Naive method: mean = " << mean << ", first = " << first - << ", last = " << last << endl; - - v_cartesian_to_polar(mag, phase, source, N); - - total = 0; - - for (int i = 0; i < N; ++i) { - if (i == 0) first = mag[i]; - if (i == N-1) last = phase[i]; - total += mag[i]; - total += phase[i]; - } - mean = total / (N*2); - cerr << "v_cartesian_to_polar: mean = " << mean << ", first = " << first - << ", last = " << last << endl; - - int iterations = 10000; -// cerr << "Iterations: " << iterations << endl; - -// cerr << "CLOCKS_PER_SEC = " << CLOCKS_PER_SEC << endl; float divisor = float(CLOCKS_PER_SEC) / 1000.f; - clock_t start = clock(); + double first, last, total = 0; for (int j = 0; j < iterations; ++j) { - for (int i = 0; i < N; ++i) { - mag[i] = sqrt(source[i].re * source[i].re + source[i].im * source[i].im); - phase[i] = atan2(source[i].im, source[i].re); - } + for (int i = 0; i < N; ++i) { + double mag = sqrt(source[i].re * source[i].re + source[i].im * source[i].im); + double phase = atan2(source[i].im, source[i].re); + if (i == 0) first = mag; + if (i == N-1) last = phase; + total += mag; + total += j * phase; + } } clock_t end = clock(); - cerr << "Time for naive method: " << float(end - start)/divisor << endl; + cerr << "naive method: first = " << first << ", last = " << last + << ", total = " << total << endl; + cerr << "time for naive method: " << float(end - start)/divisor << endl; start = clock(); + first = last = total = 0; for (int j = 0; j < iterations; ++j) { v_cartesian_to_polar(mag, phase, source, N); + for (int i = 0; i < N; ++i) { + if (i == 0) first = mag[i]; + if (i == N-1) last = phase[i]; + total += mag[i]; + total += j * phase[i]; + } } - + end = clock(); - cerr << "Time for v_cartesian_to_polar: " << float(end - start)/divisor << endl; + cerr << "v_cartesian_to_polar: first = " << first << ", last = " << last + << ", total = " << total << endl; + cerr << "time for v_cartesian_to_polar: " << float(end - start)/divisor << endl; + deallocate(source); + deallocate(mag); + deallocate(phase); + + cerr << endl; return true; } int main(int, char **) { + cerr << endl; if (!testMultiply()) return 1; if (!testPolarToCart()) return 1; if (!testPolarToCartInterleaved()) return 1;