Hey guys i am doing a basic swap program with generic pointers and I am trying to time it but I keep getting 0. Any idea why?
Code:#include <iostream> #include <iomanip> #include <ctime> using namespace std; void q(void *a, void *b, int n) { clock_t c0, c1; unsigned char *ca = (unsigned char *)a; unsigned char *cb = (unsigned char *)b; unsigned char c; c0 = clock(); for (int i = 0; i < n; i++) { c = ca[i]; ca[i] = cb[i]; cb[i] = c; } c1 = clock(); cout << "Process time is " << (double)(c1-c0)/CLOCKS_PER_SEC << " secs" << endl; } int main() { int a = 4, b = 7; float x = 2.5f, y = 3.5f; q(&a, &b, sizeof(int)); q(&x, &y, sizeof(float)); cout << setprecision(1) << fixed; cout << "(a,b) = (" << a << ',' << b << ')' << endl; cout << "(x,y) = (" << x << ',' << y << ')' << endl; }



1Likes
LinkBack URL
About LinkBacks




