PasteSite is open to the public, but with limited features. Register to be able to modify access rights, track your pastes and more...
If you prefer reading light text on a dark background to dark text on a light background, then you might want to try the dark theme.
"Untitled" by Anonymous [C/C++]Actions: |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
#include <iostream> #include <time.h> using namespace std; // We run the loops a million times so that the numbers really stand out. // In reality a single memory allocation or deallocation or the write speed of something is insignificant. // What really matters is what the result is when things pile up, and they ALWAYS pile up. #define CYCLES 1000000 int *cPtr; int cRef; void squareMePtr(int a, int *b) // It looks uglier! { *b = a*a; } void squareMeRef(int a, int &b) { b = a*a; } /* Allocate memory to a pointer, write data to it, delete the pointer */ void mainPtr() { int a = 2, *bPtr = 0; bPtr = new int; // memory allocation, this program will allocate this memory 1 MILLION times. That is a lot of allocating. squareMePtr(a, bPtr); delete bPtr; // dealloc, this deallocates 1 MILLION times as well, this is a lot of deallocation. } /* Create a reference, then write information to it */ void mainRef() { int a = 2; int bRef = 0; squareMeRef(a, bRef); } /* Write information to a pointer that exists already */ void globalPtr() { int a = 2; squareMePtr(a, cPtr); } /* Write information to a reference that exists already */ void globalRef() { int a = 2; squareMeRef(a, cRef); } int main(int argc, char *argv[]) { // Here we allocate the memory to the global pointer, this happens only once! cPtr = new int; clock_t time_a = clock(); // look at the time before the loop. // run the loop one million times. for(int i = 0; i < CYCLES; i++) { mainPtr(); } // subtract the difference in time now, to the time before the loop. // this tells us how long it took to run the loop. clock_t time_ptr = clock() - time_a; // rinse, repeat... time_a = clock(); for(int i = 0; i < CYCLES; i++) { mainRef(); } clock_t time_ref = clock() - time_a; time_a = clock(); for(int i = 0; i < CYCLES; i++) { globalPtr(); } clock_t time_glob_ptr = clock() - time_a; time_a = clock(); for(int i = 0; i < CYCLES; i++) { globalRef(); } clock_t time_glob_ref = clock() - time_a; // Output our data so we can stare in awe. cout << "Ptr Time: " << (double)time_ptr/CLOCKS_PER_SEC << endl; cout << "Ref Time: " << (double)time_ref/CLOCKS_PER_SEC << endl; cout << "Global Ptr: " << (double)time_glob_ptr/CLOCKS_PER_SEC << endl; cout << "Global Ref: " << (double)time_glob_ref/CLOCKS_PER_SEC << endl; // Note that you can't define a reference as so: // int &a_ref; // they have to be initialized. To initialize them we need to have them REFERENCE something. // so, int not_a_ref = 123; int &a_ref = not_a_ref; // Compare the two now: // the values stored. cout << "not_a_ref: " << not_a_ref << " vs. a_ref: " << a_ref << endl; // the memory address where they are stored cout << "¬_a_ref: " << ¬_a_ref << " vs. &a_ref " << &a_ref << endl; // THEY ARE THE SAME THING! And, as such, changes made to a_ref are reciprocated in not_a_ref, and vice versa. not_a_ref++; cout << "not_a_ref: " << not_a_ref << " vs. a_ref: " << a_ref << endl; a_ref+=2; cout << "not_a_ref: " << not_a_ref << " vs. a_ref: " << a_ref << endl; // See? Useful. cout << "-------For Format Sake ------" << endl; // this is quite different from the behavior of something like int a = 123, b; b = a; cout << "a: " << a << " vs. b " << b << endl; cout << "&a: " << &a << " vs. &b: " << &b << endl; // The memory addresses are not the same, and if I make changes to b, the changes are not reciprocated! They are two different // variables holding the same value, that is all. a++; cout << "a++ ==> a: " << a << " vs. b " << b << endl; b += 2; cout << "b+=2 ==> a: " << a << " vs. b " << b << endl; return 0; } |