//This program is intended to demonstrate simple serial programming where a vectors element gets modified serially. // #include <iostream> #include <vector> #include <cmath> #include <chrono> using std::cout; using std::endl; using std::vector; void increase_magnitude(float *starting_address,unsigned int size_vec, float mag_multiplyer) { //the variable *starting_address is a pointer which will point to (or contain address of), the first element of the array. for (int i=0;i<size_vec;i++) { //let us multiply the whole vector by 2 //since we have address of first element of vector, we can take its value by use of * *starting_address=*starting_address * mag_multiplyer; //now we shall increase its address by 1. starting_address+=1; } //in this way at the same memory location we will have modified the vector. } double magnitude_finder(float *starting_address_vec,unsigned int size) { //This function will return the magnitude of the vector. double sum=0.0; for (int i=0;i<size;i++) { sum+=pow((*starting_address_vec),2); starting_address_vec+=1; } return pow(sum,0.5); } int main(int argc, char *argv[]) { auto time_0 = std::chrono::high_resolution_clock::now(); unsigned int N=1<<31; //the number implies 31 zeroes in front of 1. So it is 2^31. auto time_1 = std::chrono::high_resolution_clock::now(); vector<float> vec; //whenever we declare an vector, it has undefined size(if we don't provide the size). Unless we declare it with some size. //let us fill the vector with N natural numbers(1,2,.......N). auto time_2 = std::chrono::high_resolution_clock::now(); auto elapsed_time_1 = std::chrono::duration_cast<std::chrono::microseconds>(time_2 - time_1).count() / 1e6; auto elapsed_time_storing_N = std::chrono::duration_cast<std::chrono::microseconds>(time_1 - time_1).count() / 1e6; for (int i=0;i<N;i++) { vec.push_back(i); } auto time_3 = std::chrono::high_resolution_clock::now(); auto elapsed_time_assigning_values = std::chrono::duration_cast<std::chrono::microseconds>(time_3 - time_2).count() / 1e6; //let us define a pointer to integer vector float *ptr_to_vec=&vec[0]; //giving address of first element. Or we can just write: //we shall modify the vector by multiply it with some real number. //Real numbers are stored in float data types(require 4byte per real number) and double data types(8bytes). float multiplier=4.0; //Let us find the magnitude before changing the vector, double mag_before=magnitude_finder(ptr_to_vec,N); auto time_4 = std::chrono::high_resolution_clock::now(); auto elapsed_time_mag1 = std::chrono::duration_cast<std::chrono::microseconds>(time_4 - time_3).count() / 1e6; increase_magnitude(ptr_to_vec,N,multiplier); auto time_5 = std::chrono::high_resolution_clock::now(); auto elapsed_time_modify_mag = std::chrono::duration_cast<std::chrono::microseconds>(time_5 - time_4).count() / 1e6; double mag_after=magnitude_finder(ptr_to_vec,N); auto time_6 = std::chrono::high_resolution_clock::now(); auto elapsed_time_mag2 = std::chrono::duration_cast<std::chrono::microseconds>(time_6 - time_5).count() / 1e6; double ratio_of_magnitudes=mag_after/mag_before; printf("Value of magnitude before multiplying the vector is %0.f \n",mag_before); printf("Value of magnitude after multiplying the vector is %0.f \n",mag_after); cout<<"the final vector has magnitude "<<ratio_of_magnitudes<<" times the earlier one"<<endl<<"which should be equal to the multiplier (which is: "<<multiplier<<")"<<endl; cout<<"the time to assign N: "<<elapsed_time_storing_N<<" seconds"<<endl; cout<<"the time to declare vector of N with doubles data type : "<<elapsed_time_1<<" seconds"<<endl; cout<<"the time to assign vector: "<<elapsed_time_assigning_values<<" seconds"<<endl; cout<<"the time to find magnitude of vector_in: "<<elapsed_time_mag1<<" seconds"<<endl; cout<<"the time to modify the vector_in: "<<elapsed_time_modify_mag<<" seconds"<<endl; cout<<"the time to calculate magnitude of modified vector_in: "<<elapsed_time_mag2<<" seconds"<<endl; cout<<" The total time shall be :"<<elapsed_time_1+elapsed_time_assigning_values+elapsed_time_mag1+elapsed_time_modify_mag+elapsed_time_mag2<<" seconds"<<endl; return 0; }
Output:
```
Value of magnitude before multiplying the vector is 57455839005302
Value of magnitude after multiplying the vector is 229823356021209
the final vector has magnitude 4 times the earlier one
which should be equal to the multiplier (which is: 4)
the time to assign N: 0 seconds
the time to declare vector of N with doubles data type : 0 seconds
the time to assign vector: 85.9075 seconds
the time to find magnitude of vector_in: 47.2924 seconds
the time to modify the vector_in: 4.6991 seconds
the time to calculate magnitude of modified vector_in: 47.2303 seconds
The total time shall be :185.129 seconds
real 3m5.443s
user 2m58.632s
sys 0m6.317s
```
Remarks: Note that the time taken in assigning vector and finding magnitude is very large