I think both should have the same speed, but just wanted to confirm
Which is faster - int or long
Page 1 of 14 Replies - 868 Views - Last Post: 07 September 2012 - 06:33 AM
#1
Which is faster - int or long
Posted 07 September 2012 - 03:25 AM
I was wondering which one was faster for calculations - int or long. I know floating point arithmetic is slower, but I have no clue about int and long.
I think both should have the same speed, but just wanted to confirm
I think both should have the same speed, but just wanted to confirm
Replies To: Which is faster - int or long
#2
Re: Which is faster - int or long
Posted 07 September 2012 - 03:34 AM
> I know floating point arithmetic is slower
Well this is about 20 years out of date.
There are NO blanket statements you can make regarding x is faster than y.
For example, on processors with separate integer and FP hardware, a smart compiler can interleave integer and FP instructions. By using both, you could be getting your FP answer for free.
At the other end of the scale, on some small embedded micro with no FP hardware, you'd need a damn good reason to invoke the expensive FP emulation library.
You have to look at:
- the processor(s) you're targeting.
- the optimisation features of your compiler(s).
- the nature of the code you're writing.
Change anything, and you could swing the balance one way or another.
If you're programming in a desktop environment, then use the types which make most sense. At least up to the point where you've got something reasonably complete and tested. Then you can start profiling to find out where the REAL hotspots are.
Well this is about 20 years out of date.
There are NO blanket statements you can make regarding x is faster than y.
For example, on processors with separate integer and FP hardware, a smart compiler can interleave integer and FP instructions. By using both, you could be getting your FP answer for free.
At the other end of the scale, on some small embedded micro with no FP hardware, you'd need a damn good reason to invoke the expensive FP emulation library.
You have to look at:
- the processor(s) you're targeting.
- the optimisation features of your compiler(s).
- the nature of the code you're writing.
Change anything, and you could swing the balance one way or another.
If you're programming in a desktop environment, then use the types which make most sense. At least up to the point where you've got something reasonably complete and tested. Then you can start profiling to find out where the REAL hotspots are.
#3
Re: Which is faster - int or long
Posted 07 September 2012 - 06:11 AM
In addition to what Salem said, it should also be pointed out that on very many platforms, int and long are the same size. On such platforms there is no reason why there should be any performance difference between them.
In addition to that on most CPUs operations on all types of integers take the same time independently of the integer's size - as long as it's smaller than or equal to the CPU's word size.
In addition to that on most CPUs operations on all types of integers take the same time independently of the integer's size - as long as it's smaller than or equal to the CPU's word size.
This post has been edited by sepp2k: 07 September 2012 - 06:12 AM
#4
Re: Which is faster - int or long
Posted 07 September 2012 - 06:16 AM
With the risk to be repetitive, I have read many time that nowday this kind of optimisation is well done by compiler.
I think that the choice of long is more semantic.
I think that the choice of long is more semantic.
#5
Re: Which is faster - int or long
Posted 07 September 2012 - 06:33 AM
malerv, on 07 September 2012 - 03:16 PM, said:
With the risk to be repetitive, I have read many time that nowday this kind of optimisation is well done by compiler.
What kind of optimization? On most platforms there's nothing to optimize here.
If you're talking about a platform where int and long have different sizes and operations on longs would take longer (a 16-bit platform where sizeof(int) is 2 and sizeof(long) is 4 for example), then no: The compiler won't replace long with int where it's possible. It would be impossible for the compiler to know whether a variable will ever take a value that would overflow an int (and of course it would be illegal to replace long with int without knowing this).
Page 1 of 1

New Topic/Question
Reply


MultiQuote



|