Created
October 13, 2014 23:46
-
-
Save zhangce/4ffc37523d98d623d402 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <chrono> | |
| #include <ctime> | |
| using namespace std; | |
| // double n=4 << 21; | |
| long n=1E3 * 1E3 * 1E3; | |
| const long cnt=1e8; | |
| double fast(const double* const a) { | |
| register double sum1=0; | |
| register double sum2=0; | |
| register double sum3=0; | |
| register double sum4=0; | |
| for(long i=0; i<cnt; i+=4){ | |
| sum1+=a[i]; | |
| sum2+=a[i+1]; | |
| sum3+=a[i+2]; | |
| sum4+=a[i+3]; | |
| } | |
| return sum1+sum2+sum3+sum4; | |
| } | |
| double slow(double* a) { | |
| double sum=0; | |
| long index; | |
| for(long i=0; i<cnt; i++){ | |
| index = (index + 53) % n; //CHANGE | |
| sum+=a[index]; | |
| } | |
| return sum; | |
| } | |
| int main(){ | |
| double * a=new double [n]; | |
| for(int i=0; i<cnt; i++){ | |
| a[i]=i; | |
| } | |
| volatile double kk; | |
| std::chrono::time_point<std::chrono::system_clock> start, end; | |
| start = std::chrono::system_clock::now(); | |
| kk = fast(a); | |
| end = std::chrono::system_clock::now(); | |
| std::chrono::duration<double> elapsed_seconds = end-start; | |
| double time=elapsed_seconds.count(); | |
| double mem=sizeof(double)*cnt/1024.0/1024.0/1024.0; | |
| cout << mem << " " << time << " " << mem/time << endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment