(传说RDTSC计时会不准确)
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 | #include<iostream> using namespace std; unsigned long long RDTSC() { __asm( "RDTSC" ); } bool algorithm1(unsigned long long a, int length=10) //惊悚算法 { int map[]={2,3,5,7,11,13,17,19,23,29}; unsigned long long foo=1; if (a>9876543210) return false ; for ( int i=0;i<length;++i) { if (foo % map[a%10] ==0) { return false ; } foo*=map[a%10]; a/=10; } return true ; } bool algorithm2(unsigned long long a, int length=10) { bool hash[10]={0}; if (a>9876543210) return false ; for ( int i=0;i<length;++i) { if (hash[a%10]) return false ; hash[a%10]= true ; a/=10; } return true ; } int main() { unsigned long long n; cin>>n; bool a,b; unsigned long long t1,t2,t3,t4; t1=RDTSC(); t2=RDTSC(); a=algorithm1(n,5); t3=RDTSC(); b=algorithm2(n,5); t4=RDTSC(); cout<< "algorithm1: " <<a<< " " << t3-t2-(t2-t1) <<endl; cout<< "algorithm2: " <<b<< " " << t4-t3-(t2-t1) <<endl; return 0; } |
发表评论