• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Dennis Technologies

long vs int performance

1 post in this topic

Thought I would share a recent micro-optimization I found for Android development (targeting Dalvik VM, not native). While trying to optimize some drawing code I noticed that Dalvik uses 32bit registers and thus register pairs (i.e. two registers per) for storage of 64bit data types like the scalar 'long'. Feeling that there might be a performance impact for having to access two registers, I decided to do some benchtesting with the following code:

		public void run() {
				int intCounter = 0;
				int i;
				long longCounter = 0;
				long ii;
				long elapseTime;
				long startTime;
				
				startTime = SystemClock.uptimeMillis();
				
				for (int counter = 0; counter < 100; counter++) {
					for (i = 0; i < 100000; i++) {
						intCounter += i;
					}
					intCounter += counter;
				}
				
				elapseTime = SystemClock.uptimeMillis() - startTime;
			
				Log.d("IntLongTest", "Integer Ending Value - " + intCounter + " / Total Time - " + elapseTime);
				
				startTime = SystemClock.uptimeMillis();
				
				for (long counter = 0; counter < 100; counter++) {
					for (ii = 0; ii < 100000; ii++) {
						longCounter += ii;
					}
					longCounter += counter;
				}
				
				elapseTime = SystemClock.uptimeMillis() - startTime;
				
				Log.d("IntLongTest", "Long Ending Value - " + longCounter + " / Total Time - " + elapseTime);
			}

I came up with the following results:

09-14 23:11:18.567: D/IntLongTest(9491): Integer Ending Value - 1778798614 / Total Time - 123
09-14 23:11:18.728: D/IntLongTest(9491): Long Ending Value - 499995004950 / Total Time - 159

09-14 23:11:27.016: D/IntLongTest(9491): Integer Ending Value - 1778798614 / Total Time - 81
09-14 23:11:27.197: D/IntLongTest(9491): Long Ending Value - 499995004950 / Total Time - 175

09-14 23:11:34.044: D/IntLongTest(9491): Integer Ending Value - 1778798614 / Total Time - 33
09-14 23:11:34.214: D/IntLongTest(9491): Long Ending Value - 499995004950 / Total Time - 163

09-14 23:11:42.653: D/IntLongTest(9491): Integer Ending Value - 1778798614 / Total Time - 39
09-14 23:11:42.843: D/IntLongTest(9491): Long Ending Value - 499995004950 / Total Time - 193

 

The average execution time for integers is 68.75ms while the average execution time for longs is 172.5. Which means that longs are roughly 2.5 times slower than integers on my Galaxy S3 in this test. I do acknowledge it would be fairly unlikely that this would be a sole reason for poor performance in your code, but I thought it might be an interesting little tidbit :)

 

This did help me improve draw performance by roughly 10% because I was using longs to track animation frame dwell times for each individual sprite on the screen (I was benchtesting with 10,000 sprites).

 

Side note, you will notice the integer value is different from the long value which I believe is due to 'roll over' since the loop would take the integer variable past its maximum value.
 

If anybody else sees a different explanation I would love to hear it. This is just an idle observation, I have not ripped open the dex to see if the bytecode is acting different than expected or if the JIT is helping integers out somehow.

0

Share this post


Link to post
Share on other sites
Okay...

Android machines are 32-bit. This should not be a surprise.

Of course the cost of a single operation on a 64-bit value on a 32-bit processor is going to be anywhere from 2x to 6x as expensive. Bitwise operations can be done on the upper and lower halves, so they take two steps. Many other operations require three steps. Addition means you add the lower half, add the upper half, and add the carried information from the lower half, if any. Subtraction is the same. Multiplication is five steps, division is potentially six steps.

I don't see how or why that would be surprising to anyone. If you're a university graduate I expect you would have taken a theory of computing class that covered it, a compiler theory class that covered it, and possibly additional courses on data processing or information theory that covered it. Even if you didn't attend school, it should be apearent that doing more work takes more time.

Use the right variable size for the underlying value. If you need a 64-bit variable, then use it.



Also, I'm not sure what you want people to do with the information. Just because operations on a single variable take more time does not mean the operations are a performance bottleneck. Usually the actual bottlenecks in performance are not the things you expect. The only way to know for certain is to measure which thing is slowing you down.

The first round of big bottlenecks are things like the unexpected N^4 loop that you didn't know was present, or the cases where you allocate tens of thousands of objects and throw them away. The second big round are cases where you are going through the entire data set with an n^2 algorithm where you really can prune it down to a small number of less-costly tests, or hop all over memory where a sequential algorithm is available. Subsequent rounds of profiling will identify issues that are specific to each code base.

It is unlikely to have a situation where a 64-bit value is needed where at the same time the 64-bit math is also an actual bottleneck.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0