Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Integer and float benchmark for Win32 and Win64
- // Results are below main(), line 91
- #include <stdlib.h>
- #include <stdio.h>
- #ifdef _WIN32
- #include <sys/timeb.h>
- #else
- #include <sys/time.h>
- #endif
- #include <time.h>
- double
- mygettime(void) {
- # ifdef _WIN32
- struct _timeb tb;
- _ftime(&tb);
- return (double)tb.time + (0.001 * (double)tb.millitm);
- # else
- struct timeval tv;
- if(gettimeofday(&tv, 0) < 0) {
- perror("oops");
- }
- return (double)tv.tv_sec + (0.000001 * (double)tv.tv_usec);
- # endif
- }
- template< typename Type >
- void my_test(const char* name) {
- volatile Type v = 0;
- // Do not use constants or repeating values
- // to avoid loop unroll optimizations.
- // All values >0 to avoid division by 0
- Type v0 = (Type)(rand() % 256)/16 + 1;
- Type v1 = (Type)(rand() % 256)/16 + 1;
- Type v2 = (Type)(rand() % 256)/16 + 1;
- Type v3 = (Type)(rand() % 256)/16 + 1;
- Type v4 = (Type)(rand() % 256)/16 + 1;
- Type v5 = (Type)(rand() % 256)/16 + 1;
- Type v6 = (Type)(rand() % 256)/16 + 1;
- Type v7 = (Type)(rand() % 256)/16 + 1;
- double t1 = mygettime();
- for (size_t i = 0; i < 100000000; ++i) {
- v += v0;
- v += v2;
- v += v4;
- v += v6;
- }
- printf("%s add: %f\n", name, mygettime() - t1);
- t1 = mygettime();
- for (size_t i = 0; i < 100000000; ++i) {
- v -= v1;
- v -= v3;
- v -= v5;
- v -= v7;
- }
- printf("%s sub: %f\n", name, mygettime() - t1);
- t1 = mygettime();
- for (size_t i = 0; i < 100000000; ++i) {
- v *= v0;
- v *= v2;
- v *= v4;
- v *= v6;
- }
- printf("%s mul: %f\n", name, mygettime() - t1);
- t1 = mygettime();
- for (size_t i = 0; i < 100000000; ++i) {
- v /= v1;
- v /= v3;
- v /= v5;
- v /= v7;
- }
- printf("%s div: %f\n", name, mygettime() - t1);
- }
- int main() {
- my_test< short >(" short");
- my_test< long >(" long");
- my_test< long long >("long long");
- my_test< float >(" float");
- my_test< double >(" double");
- return 0;
- }
- ///////////////////////////////////////////////////////
- Win32:
- short add: 0.889000
- short sub: 0.892000
- short mul: 1.182000
- short div: 3.352000
- long add: 0.885000
- long sub: 0.884000
- long mul: 1.178000
- long div: 3.381000
- long long add: 1.152000
- long long sub: 1.135000
- long long mul: 1.971000
- long long div: 4.484000
- float add: 1.423000
- float sub: 1.432000
- float mul: 50.639000
- float div: 52.085000
- double add: 1.425000
- double sub: 1.425000
- double mul: 50.774000
- double div: 51.748000
- Win64:
- short add: 1.204000
- short sub: 1.249000
- short mul: 1.437000
- short div: 3.590000
- long add: 0.856000
- long sub: 0.855000
- long mul: 1.146000
- long div: 3.291000
- long long add: 0.876000
- long long sub: 0.861000
- long long mul: 1.145000
- long long div: 6.035000
- float add: 1.422000
- float sub: 1.419000
- float mul: 1.555000
- float div: 1.997000
- double add: 1.421000
- double sub: 1.415000
- double mul: 1.714000
- double div: 2.002000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement