Why is `float` so much faster than `int` in this program?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Why is `float` so much faster than `int` in this program?

  1. #1
    Join Date
    May 2017
    Posts
    1

    Why is `float` so much faster than `int` in this program?

    This simple program copy data from the array `xy` to `arr` and prints the time it takes
    Code:
        #include <stdio.h>
        #include <time.h>
        
        class Point
        {
        public:
        	float x, y;
        
        	Point() :x(0), y(0) {}
        	Point(float x, float y) : x(x), y(y) {}
        };
        
        int main()
        {
        	const size_t n = 99999999;
        	Point *arr = new Point[n];
        
        	float *xy = new float[2 * n];
        	for (size_t i = 0; i < 2 * n; i++)
        		xy[i] = (float)i;
        
        	clock_t start = clock();
        
        	for (size_t i = 0, j = 0; i < n; i++)
        	{
        		float x = xy[j++];
        		float y = xy[j++];
        		arr[i] = Point(x, y);
        	}
        
        	clock_t end = clock();
        
        	printf("time: %d\n", (end - start));
        
        	delete[] arr;
        	delete[] xy;
        
        	return 0;
        }

    It takes about 250 ms. If I change everything to `int`:
    Code:
        #include <stdio.h>
        #include <time.h>
        
        class Point
        {
        public:
        	int x, y;
        
        	Point() :x(0), y(0) {}
        	Point(int x, int y) : x(x), y(y) {}
        };
        
        int main()
        {
        	const size_t n = 99999999;
        	Point *arr = new Point[n];
        
        	int *xy = new int[2 * n];
        	for (size_t i = 0; i < 2 * n; i++)
        		xy[i] = (int)i;
        
        	clock_t start = clock();
        
        	for (size_t i = 0, j = 0; i < n; i++)
        	{
        		int x = xy[j++];
        		int y = xy[j++];
        		arr[i] = Point(x, y);
        	}
        
        	clock_t end = clock();
        
        	printf("time: %d\n", (end - start));
        
        	delete[] arr;
        	delete[] xy;
        
        	return 0;
        }
    it takes almost 3 times the time, around 700 ms. Why is there is a difference? I expected it to be the same because they are both 32 bits and there are no arithmetic operations involved.
    I am using Visual Studio 2015, Configuration: Release x64. Processor: intel i7-4510U.

  2. #2
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    696

    Re: Why is `float` so much faster than `int` in this program?

    I run the following code on a 32 bit processor, the timings are identical.

    Code:
    #include <stdio.h>
    #include <time.h>
    
    class PointInt
    {
    public:
    	int x, y;
    
    	PointInt() :x(0), y(0) {}
    	PointInt(float x, float y) : x(x), y(y) {}
    };
    
    class PointFloat
    {
    public:
    	float x, y;
    
    	PointFloat() :x(0), y(0) {}
    	PointFloat(int x, int y) : x(x), y(y) {}
    };
    
    int main()
    {
    	const size_t n = 10000000;
    	PointInt *arr1 = new PointInt[n];
    	PointFloat *arr2 = new PointFloat[n];
    
    	int *xy1 = new int[2 * n];
    	float *xy2 = new float[2 * n];
    
    	for (size_t i = 0; i < 2 * n; i++)
    		xy1[i] = (int)i, xy2[i] = (float)i;
    
    	clock_t start1 = clock();
    	for (size_t i = 0, j = 0; i < n; i++)
    	{
    		int x = xy1[j++];
    		int y = xy1[j++];
    		arr1[i] = PointInt(x, y);
    	}
    	clock_t end1 = clock();
    	printf("time: %d\n", (end1 - start1));
    
    	clock_t start2 = clock();
    
    	for (size_t i = 0, j = 0; i < n; i++)
    	{
    		float x = xy2[j++];
    		float y = xy2[j++];
    		arr2[i] = PointFloat(x, y);
    	}
    
    	clock_t end2 = clock();
    	printf("time: %d\n", (end2 - start2));
    
    	delete[] arr1;
    	delete[] xy1;
    	delete[] arr2;
    	delete[] xy2;
    
    	return 0;
    }
    Probably, this is caused due to more improved floating point command set of the 64 architecture.
    Last edited by AvDav; May 28th, 2017 at 01:22 AM. Reason: corrected

  3. #3
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,785

    Re: Why is `float` so much faster than `int` in this program?

    For the code in post #2 for my Windows 7 64 bit using VS2017 in release mode with full optimisations enabled

    For 32-bit .exe
    Code:
    int time: 21
    float time: 20
    For 64 bit .exe
    Code:
    int time: 11
    float time: 58
    Why is `float` so much faster than `int`
    What optimisations are enabled for the project properties?
    Last edited by 2kaud; May 27th, 2017 at 06:29 AM.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.4.0)

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,785

    Re: Why is `float` so much faster than `int` in this program?

    Eh, no. In the code in post #2, PointInt is dealing with float and PointFloat is dealing with int!

    Consider
    Code:
    #include <iostream>
    #include <ctime>
    #include <typeinfo>
    using namespace std;
    
    using fp = float;
    //using fp = double;
    
    using in = _int32;
    //using in = _int64;
    
    const size_t n = 10000000;
    const size_t n2 = n * 2;
    
    template<typename T>
    class Point
    {
    public:
    	T x{}, y{};
    
    	Point() = default;
    	Point(T x1, T y1) : x(x1), y(y1) {}
    };
    
    template<typename T>
    size_t timing(const T* xy)
    {
    	const auto arrbeg = new Point<T>[n];
    	auto arr = arrbeg;
    
    	clock_t start1 = clock();
    	for (size_t i = 0; i < n; ++i)
    	{
    		auto x = *xy++;
    		auto y = *xy++;
    		*arr++ = Point<T>(x, y);
    	}
    	clock_t end1 = clock();
    
    	delete[] arrbeg;
    
    	return end1 - start1;
    }
    
    int main()
    {
    	auto xyi = new in[n2];
    	auto xyf = new fp[n2];
    
    	for (size_t i = 0; i < n2; ++i)
    		xyi[i] = (in)i, xyf[i] = (fp)i;
    
    	cout << typeid(in).name() << " time " << timing(xyi) << endl;
    	cout << typeid(fp).name() << " time " << timing(xyf) << endl;
    
    	delete[] xyi;
    	delete[] xyf;
    
    	return 0;
    }
    For 64 bit .exe
    Code:
    int time: 56
    float time: 12
    For 32 bit .exe
    Code:
    int time: 11
    float time: 22
    Changing type float to type double gives
    For 64 bit .exe
    Code:
    int time: 57
    double time: 23
    For 32 bit .exe
    Code:
    int time: 11
    double time: 24
    Last edited by 2kaud; May 27th, 2017 at 09:09 AM.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.4.0)

  5. #5
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    696

    Re: Why is `float` so much faster than `int` in this program?

    Accept my apologies, didn't want to mislead anyone.

  6. #6
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    696

    Re: Why is `float` so much faster than `int` in this program?

    I ran the code shown by 2kaud in his original posting no. 4, the timings are nearly equal on a 32-bit machine.
    OMG, I need to update my CPU as the speed is relatively slow.
    Popular opinion is the greatest lie in the world.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)