C++ real number, integer speed difference

Asked 2 years ago, Updated 2 years ago, 24 views

When using the same algorithm in C++, does the operation show a significant difference depending on whether it is a real or integer operation?

For example, if you find the area of a circle in a loop 100 million times, is it possible to make a mistake in radius or is it limited to integers?

c++

2022-09-21 17:09

2 Answers

There is a question in stackoverflow like . It depends on the preprocessor.

In my case, 4294967295, the int operation was about two seconds faster.

#include<iostream>
#include<cmath>
#include<ctime>
#include<limits>

using namespace std;
const unsigned long max_count = numeric_limits<unsigned int>::max();

int calc_int(int radius){
    return (0.5*radius*radius*M_PI);
}

float calc_float(float radius){
    return (0.5*radius*radius*M_PI);
}


double calc_double(double radius){
    return (0.5*radius*radius*M_PI);
}

int main(void){
    cout << "The number of repetitions is ""<< max_count <<"" <<< endl;;;
    clock_t start, end;

    // Double type
    start = clock();
    double double_num = 0.0;
    for(long i=0; i<max_count; i++, double_num++)
        calc_int(double_num);
    end = clock();
    Cout < < "a performance time : double" < < clocks and ((end start) _ per sec) < <. ; endl


    A // float
    start = clock();
    float float_num = 0.0;
    for(long i=0; i<max_count; i++, float_num++)
        calc_int(float_num);
    end = clock();
    Cout < < "a performance time : float" < < clocks and ((end start) _ per sec) < <. ; endl


    A // int
    start = clock();
    int int_num = 0;
    for(long i=0; i<max_count; i++, int_num++){
        calc_int(int_num);
    }
    end = clock();
    cout<<"int type execution time:"<<(end-start)/CLOCKS_PER_SEC)<<<endl;

}
The number of iterations is 4294967295.
Double type execution time: 17
Float type execution time: 17
int type execution time: 14


2022-09-21 17:09

-1. If you're thinking of accurate calculations, you have to do it in an integer type.

-2. Due to the nature of the computer, the real type is treated as binary, so errors accumulate as the computation continues.

#include <stdio.h>
int main(){
    float a = 0.0001,b=0;
    int i;
    for(i=0;i<1000;i++) {
        b = b + a;
    }
    printf("b = %e",b);
    printf("b = %f",b);
    return 0;
}

In the example above, float has a very small number of bits to represent the effective digits, which makes it more pronounced. If you add 0.0001 1000 times, it should be 0.1, but if you look up, you can see that it doesn't get 0.1.

-3. You have to use mistakes semantically, and if you want accuracy, you have to use double type. Even in this case, when you expect the result of any calculation, you should calculate the effective position and check if it is correct. For example, you need to write a code that checks which real numbers result is only five decimal places.

-4. For the same reasons as in 2 and 3 above, mistakes should not be used when the count variable of ex. for loop must be accurately counted.

-5. The speed is basically faster than real-world calculations if the hardware of our computers uses the features that are included in the same device. In practice, hardware for floating-point (e.g. GPUs) for floating-point calculations can sometimes be seen as faster performance, which depends on which hardware you use. Not only that, but depending on the size of the data (16 bit, 32 bit, 64 bit...) the number of access to the memory varies, so the algorithms (or programs) we use may not be as different as we think, so it depends.


2022-09-21 17:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.