-4

I am making a 2D shooter game, and thus I have to stuff in a array lots of bullets, including their position, and where they are going.

So I have two issues, one is memory use, specially writing arrays that don't place things out of aligned and results in lots of padding or alignment that makes the speed of calculations suck.

The second is speed of calculation.

First this mean between choosing integers or floats... For now I am going with integers (if someone think floating point is better, please say so).

Then, this also mean choosing a variant of that type (8 bits? 16 bits? C confusing default? The CPU word size? Single precision? Double precision?)

Thus the question is: What type in C is fastest in modern processors (ie: common x86, ARM and other popular processors, don't worry about Z80 or 36bit processors), and what type is more reasonable when taking speed AND memory use in account?

Also, signed and unsigned has differences in speed?

EDIT because of close votes: Yes, it might be premature optimization, but I am asking not only about CPU use, but memory use (that might vary significantly), also I am doing the project to exercise my C skills, it is some years I don't code in C, and I thought to have some fun and find limits and stretch them, and also learn new standards (last time I used C it was still C89).

Finally, the major motivation of asking this question was just hacker curiosity when I found out some new interesting types (like int_fast*_t) existed in newer standards.

But if you still think this is not worth asking, then I can delete the question and go peruse the standards and some books, learn by myself. Then if others one day have the same curiosity, it is not my problem.

  • 9
    How many bullets are you talking about, and how often are you updating the screen? What is your target platform? I suspect you may be overthinking it - have you done any experimentation yet to see whether this is actually going to be a problem at all? – Jon Skeet Jan 30 '14 at 00:03
  • [Readability >>>>>>>>> performance](http://stackoverflow.com/questions/21438631/fastest-way-of-computing-the-power-that-a-power-of-2-number-used#comment32349214_21438862) –  Jan 30 '14 at 00:08
  • for now target platform is x86 (Win, OSX, Linux, etc...), later I might also port it to ARM (Android, iOS) and other misc Android platforms (like... MIPS). Also I am unsure yet, I am mostly experimenting, I might use 3 bullets, but I might also go "bullet hell" and use thousands... And currently my game logic update every 8 miliseconds, but I am considering going 4 miliseconds too, I am not sure about that. –  Jan 30 '14 at 00:08
  • 5
    The choice of float vs int is usually made by your design requirements, and not as an optimisation criterion. – MicroVirus Jan 30 '14 at 00:10
  • 1
    H2CO3 this is also about memory use,not only CPU use, and memory use CAN be very different (ignoring padding, the difference between 8 bits and 32 bits or 64 is crazy gigantic, then you might have struct padding), also I asked most out of curiosity when I saw all new types in C99 –  Jan 30 '14 at 00:11
  • Also I would like to note that I am making this game as a personal C exercise, to find limits and push them (and later release the source), this project I am not caring about work efficiency –  Jan 30 '14 at 00:13
  • 2
    This does not change the fact that attempting to optimize this without checking if it's a problem first, is such a premature optimization, that it still wears diapers. I'm not saying the question isn't legit, I just don't think it's going to be useful. – Leeor Jan 30 '14 at 00:18
  • Single precision floating point add and multiply is as fast as as 32 bit integer arithmetic in all modern processors (x86,ARM,MIPS). (i.e. one result per clock cycle). Calculating positions and velocity in space is *a lot* easier with floating point arithmetic, so use `float`s. – markgz Jan 30 '14 at 00:44
  • markgz even considering the amount of bytes to fetch from the memory? (I think float by default is 32 bits and int is 16?) –  Jan 30 '14 at 00:47
  • 3
    I can't agree with all the remarks about 'premature optimization'. I don't consider choice of data types to be 'optimization' at all, it is a critical aspect of program design, and one which may be rather inconvenient to change afterwards. The answer to this question is *clearly* 'int' rather than 'float', and it would have been a lot more helpful for all those people to say so instead of embarking on this digression. And we need to remember that Don Knuth preceded his remark with a qualification, something like '99% of the time'. – user207421 Jan 30 '14 at 01:04
  • @EJP Here's what he said: http://c2.com/cgi/wiki?PrematureOptimization – Jim Balter Jan 30 '14 at 01:11
  • 1
    In reference to your desire to improve your C programming skills, how about you construct specific timed tests to benchmark one idea against the other? That will give you your answer, as well as hone your skills. – paddy Jan 30 '14 at 01:13
  • Not bad idea paddy :) –  Jan 30 '14 at 01:14
  • @JimBalter Thanks, exactly. I had forgotten the final sentence, which also applies here. For the benefit of others: "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%." – user207421 Jan 30 '14 at 01:14
  • 1
    @speeder That is generally the first thing to do when you have concerns over design decisions. The time to ask a question is when you don't understand the results of your experiments. – paddy Jan 30 '14 at 01:15

2 Answers2

7

I would say an int should be the most comfortable for your CPU. But the C standard does have:

The typedef name int_fastN_t designates the fastest signed integer type with a width of at least N . The typedef name uint_fastN_t designates the fastest unsigned integer type with a width of at least N

So in theory you could say things like: "I need it to be at least 16 bits so I shall use int_fast16_t". In practice that might translate to a plain int.


I suspect it is premature to think about these before you actually hit a performance issue that you can try to work around. I think it is better to solve problems when they occur than to try to think of an elusive super-solution that could solve all future possible issues.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
2

Single precision floating point add and multiply is as fast as as 32 bit integer arithmetic in all modern processors (x86,ARM,MIPS), i.e. one result per clock cycle. Calculating positions and velocity in space is a lot easier with floating point arithmetic, so use floats. Single precision floats are 32 bits, and are the same size as the most efficient integer type on 32 bit CPUs.

markgz
  • 6,054
  • 1
  • 19
  • 41