kemi (statz) | #2, Főfasz (2970) |
4356 | #2460 | ^ | Idézet | Sun, 17 Mar 2013 22:24:36 +01 |
94.21.*.* | *.pool.digikabel.hu |
Ugyanaz a teszt, OOP vs imperatív megoldás, default, meg -O3 kapcsolókkal fordítva: rnd2.h #ifndef RND2_H_ #define RND2_H_ void srnd2(unsigned long int seed); unsigned long int rnd2(); #endif rnd2.cpp unsigned long int __seed0, __seed1, __seed2, __seed3; unsigned long int __rol(unsigned long int value, unsigned char offset) { offset = offset & 0x1f; return (value << offset) | (value >> (0x20 - offset)); } unsigned long int __ror(unsigned long int value, unsigned char offset) { offset = offset & 0x1f; return (value >> offset) | (value << (0x20 - offset)); } void srnd2(unsigned long int seed) { __seed0 = seed; __seed1 = __seed0 ^ 0xaaaaaaaa; __seed1 = __rol(__seed1 >> 11, __seed1); __seed1 = __seed1 ^ (__seed1 >> 21); __seed2 = __seed0 ^ 0x55555555; __seed2 = __ror(__seed2 << 6, __seed2); __seed2 = __seed2 ^ (__seed2 >> 22); __seed3 = (__seed2 << 16) | (__seed1 >> 16); } unsigned long int rnd2() { __seed1 = __rol(__seed1, 5); __seed2 = __ror(__seed2, 9); __seed0 = __rol(__seed0, 11); __seed0 ^= __seed1; __seed0 ^= __rol(__seed1, 8) << 11; __seed0 ^= __ror(__seed2, 8) & 63; __seed0 ^= __rol(__seed3, 8) >> 5; __seed0 = __ror(__seed0, 14); __seed0 ^= __seed3; __seed1 ^= __seed3; __seed2 ^= __seed3; __seed3 = __ror(__seed3, __seed0); return __seed0; } imp.cpp #include <cstdlib> #include <ctime> #include "rnd2.h" int main() { srnd2(time(NULL)); int table[256]; int i, j, k, r, temp; for (i = 0; i < 1000; i++) { for (j = 0; j < 1000; j++) { for (k = 0; k < 256; k++) table[k] = k; for (k = 0; k < 256; k++) { r = rnd2() & 255; temp = table[r]; table[k] = temp; } } } return 0; } CRnd2.h #ifndef CRND2_H_ #define CRND2_H_ class CRnd2 { public: void srnd2(unsigned long int seed); unsigned long int rnd2(); private: unsigned long int __seed0; unsigned long int __seed1; unsigned long int __seed2; unsigned long int __seed3; unsigned long int __rol(unsigned long int value, unsigned char offset); unsigned long int __ror(unsigned long int value, unsigned char offset); }; #endif CRnd2.cpp #include "CRnd2.h" unsigned long int CRnd2::__rol(unsigned long int value, unsigned char offset) { offset = offset & 0x1f; return (value << offset) | (value >> (0x20 - offset)); } unsigned long int CRnd2::__ror(unsigned long int value, unsigned char offset) { offset = offset & 0x1f; return (value >> offset) | (value << (0x20 - offset)); } void CRnd2::srnd2(unsigned long int seed) { __seed0 = seed; __seed1 = __seed0 ^ 0xaaaaaaaa; __seed1 = __rol(__seed1 >> 11, __seed1); __seed1 = __seed1 ^ (__seed1 >> 21); __seed2 = __seed0 ^ 0x55555555; __seed2 = __ror(__seed2 << 6, __seed2); __seed2 = __seed2 ^ (__seed2 >> 22); __seed3 = (__seed2 << 16) | (__seed1 >> 16); } unsigned long int CRnd2::rnd2() { __seed1 = __rol(__seed1, 5); __seed2 = __ror(__seed2, 9); __seed0 = __rol(__seed0, 11); __seed0 ^= __seed1; __seed0 ^= __rol(__seed1, 8) << 11; __seed0 ^= __ror(__seed2, 8) & 63; __seed0 ^= __rol(__seed3, 8) >> 5; __seed0 = __ror(__seed0, 14); __seed0 ^= __seed3; __seed1 ^= __seed3; __seed2 ^= __seed3; __seed3 = __ror(__seed3, __seed0); return __seed0; } oop.cpp #include <cstdlib> #include <ctime> #include "CRnd2.h" int main() { CRnd2 rnd = CRnd2(); rnd.srnd2(time(NULL)); int table[256]; int i, j, k, r, temp; for (i = 0; i < 1000; i++) { for (j = 0; j < 1000; j++) { for (k = 0; k < 256; k++) table[k] = k; for (k = 0; k < 256; k++) { r = rnd.rnd2() & 255; temp = table[r]; table[k] = temp; } } } return 0; } kemi242@linuxmint ~/a $ time ./imp real 0m21.088s user 0m21.077s sys 0m0.004s kemi242@linuxmint ~/a $ time ./imp_O3 real 0m2.430s user 0m2.424s sys 0m0.000s kemi242@linuxmint ~/a $ time ./oop real 0m29.269s user 0m29.266s sys 0m0.000s kemi242@linuxmint ~/a $ time ./oop_O3 real 0m2.454s user 0m2.444s sys 0m0.004s kemi242@linuxmint ~/a $Úgy látszik, az O3 kapcsoló nagyrészt kioptimalizálja az OOP hátrányait, ahol tudja. |