4 Replies - 868 Views - Last Post: 07 September 2012 - 06:33 AM Rate Topic: -----

#1 aresh   User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,258
  • Joined: 08-January 12

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 :)
Is This A Good Question/Topic? 0
  • +

Replies To: Which is faster - int or long

#2 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2555
  • View blog
  • Posts: 4,739
  • Joined: 30-May 10

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.
Was This Post Helpful? 4
  • +
  • -

#3 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2770
  • View blog
  • Posts: 4,429
  • Joined: 21-June 11

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.

This post has been edited by sepp2k: 07 September 2012 - 06:12 AM

Was This Post Helpful? 0
  • +
  • -

#4 malerv   User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 100
  • Joined: 01-July 09

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.
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2770
  • View blog
  • Posts: 4,429
  • Joined: 21-June 11

Re: Which is faster - int or long

Posted 07 September 2012 - 06:33 AM

View Postmalerv, 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).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1