-1

What is faster: (Performance)

__int64 x,y;
x=y;

or

int x,y,a,b;
x=a;
y=b;

?

Or they are equal?

User is deleted
  • 157
  • 2
  • 11
  • There is no way to answer this definitively without knowing the architecture and the compiler. However, the first one will be at least as fast as the second one. On the other hand, the compiler is allowed to optimize the second pair of assignments to be as fast as the first assignment. – Sergey Kalinichenko Nov 04 '12 at 16:08
  • 4
    They're completely different things. The question which one is faster doesn't even apply. –  Nov 04 '12 at 16:08
  • You are comparing apples to oranges. They are no where related. – Naveen Nov 04 '12 at 16:17
  • 2
    If you want speed, why not use `int_fast64_t`? – Kerrek SB Nov 04 '12 at 16:19

4 Answers4

4

__int64 is a non-standard compiler extension, so whilst it may or may not be faster, you don't want to use it if you want cross platform code. Instead, you should consider using #include <cstdint> and using uint64_t etc. These derive from the C99 standard which provides stdint.h and inttypes.h for fixed width integer arithmetic.

In terms of performance, it depends on the system you are on - x86_64 for example should not see any performance difference in adding 32- and 64- bit integers, since the add instruction can handle 32 or 64 bit registers.

However, if you're running code on a 32-bit platform or compiling for a 32-bit architecture, the addition of a 64-bit integers will actually require adding two 32-bit registers, as opposed to one. So if you don't need the extra space, it would be wasteful to allocate it.

I have no idea if compilers can or do optimise the types down to a smaller size if necessary. I expect not, but I'm no compiler engineer.

Community
  • 1
  • 1
  • "... the addition of two 64-bit integers will actually require adding two 32-bit registers, as opposed to one." I think this is a typo... – leemes Nov 04 '12 at 16:16
4

I hate these sort of questions.

1) If you don't know how to measure for yourself then you almost certainly don't need to know the answer.

2) On modern processors it's very hard to predict how fast something will be based on single instructions, it's far more important to understand your program's cache usage and overall performance than to worry about optimising silly little snippets of code that use an assignment. Let the compiler worry about that and spend your time improving the algorithm used or other things that will have a much bigger impact.

So in short, you probably can't tell and it probably doesn't matter. It's a silly question.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
1

The compiler's optimizer will remove all the code in your example so in that way there is no difference. I think you want to know whether it is faster to move data 32 bits at a time or 64 bits at a time. If your data is aligned to 8 bytes and you are on a 64 bit machine then it should be faster to move data at 8 bytes at a time. There are several caveat to that, however. You may find that your compiler is already doing this optimization for you (you would have to look at the emitted assembly code to be sure), in which case you would see no difference. Also, consider using memcpy instead of rolling your own if you are moving a lot of data. If you are considering casting an array of 32 bit ints to 64 bit in order to copy faster or do some other operation faster (i.e. in half the number of instructions), be sure to Google for the strict aliasing rule.

Community
  • 1
  • 1
Bjarke H. Roune
  • 3,667
  • 2
  • 22
  • 26
0

__int64 should be faster on most platforms, but be careful - some of the architectures require alignment to 8 for this to take effect, and some would even crash your app if it's not aligned.

malkia
  • 1,389
  • 13
  • 12