Jump to content

Code efficiency: Which is faster?


Slocket
 Share

Recommended Posts

I have this coding problem. Maybe. I need to expand my namespace arrays to make for more data. But I am wondering about data and speed of execution after the code is compiled.

 

Does it matter if I stick all the data elements into one huge array, both static and dynamic, and read/write to each element: or,

 

Make one huge array to read in all the static data such as planet and star locations that do not change in game; and make a smaller data array that holds the variables that do change during the game, such as planet loyalty.

 

It would seem the game stores all this stuff in a memory location, so why would it care if adressing it in game is from one huge combination of elements planet.loyalty(n) ; as opposed to one single array just for that planetLoyalty(n)? :?:

 

I assume it does not matter, but as this gets bigger the code, anything that may slow it down might start to show itself. I can save alot of pain in the future; once the game is cycling through thousands of lines of code...it may make a difference.

Edited by Slocket
Link to comment
Share on other sites

  • SWR Staff - Executive

Unless the values of static elements are compiled into the executable, I don't think you're going to see any difference. All data should be allocated and deallocated when they are no longer in use. I think you're talking game data that is needed throughout, so it really doesn't matter.

 

It does matter if you are using dynamic arrays or static ones. As static ones are a fixed size, there is a lot less the program has to do. Dynamic arrays may end up copying lots of data back and forth when expanding, depending on how the arrays are implemented.

Evaders99

http://swrebellion.com/images/banners/rebellionbanner02or6.gif Webmaster

http://swrebellion.com/images/banners/swcicuserbar.png Administrator

 

Fighting is terrible, but not as terrible as losing the will to fight.

- SW:Rebellion Network - Evaders Squadron Coding -

The cake is a lie.

Link to comment
Share on other sites

Static values are read into the game from an external file upon start up. So I am good there.

 

I am only using static arrays. No dynamic arrays.

 

Data is destroyed (cleaned up) when no longer in use.

 

So I guess I am still good to go, I really did not want to retype about 4000 lines of code. It is not the best looking code, but it does work. :D

 

Thank you and anyone else for the input. I feel better to keep marching on.

Link to comment
Share on other sites

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.

 

You worry too much (and about the wrong things). If your profiler says it's bottleneck, then optimize it. Otherwise keep the design as clean as possible. Besides there's nothing wrong with using dynamic arrays. They adjust their size only when you add/remove items from them, which is not done so often (and usually these containers are very optimized).

 

And about bottlenecks. You game will mostly be slowed down by inefficient algorithms (eg. with bad time complexity) or hw constraints (waiting for GPU to finish rendering).

 

To your original question, keeping related data close together might be more cache friendly rather than keeping them separately, but again profiler is your friend here.

 

My two cents.

 

Btw. I have have kinda the opposite problem. Terra runs too fast and eats too much CPU. I'll have to implement some sort of framerate limiter to make her more gentle on my lappy's battery.

-rebellion2 enthusiast-

Terra Reconstructed

Link to comment
Share on other sites

Post your code and let me take a look at it. I agree with Moribundus though. If bottlenecked fix it. Part of it also has to do with the design pattern you are using.

 

Hopefully I read your question correctly -- but if you are taking an object-oriented approach, it's best to set all final and static variables associated with the object WITHIN that object. Here is a code snippet from Rebellion 2, take a look:

 

 

 

    /**
    * Sets the loyalty of a planet.  Must be between -10 and 10.
    * Ownership determined by outside elements.
    * @param loyalty is favor of planet to the player's faction.
    */
   public void setLoyalty(int loyalty)
   {
       this.loyalty = loyalty;
   }
   /**
    * Returns planet's loyalty.
    * The range of negative 5 to 5 is neutral.
    * @return planet's current loyalty.
    */
   public int getLoyalty()
   {
       return this.loyalty;
   }

 

The only arrays I have are ones which contain the objects: one for planets, one for Officers, and so on. And I can add and delete as necessary. This hierarchy makes memory management a LOT easier on both you and your compiler.

Link to comment
Share on other sites

  • 1 month later...

Yes that answers my question. It seems to really make no difference, array or just one big namespace.

 

I notice this. I was using to find distance upon a mouse click on screen with a specific spot in 2D interface.

 

D< SQR(X^2+y^2) now that if with bounds of that circle is AND with MouseClick(). Simple? but not efficient. x=(x1-x2) and y=(y1-y2) the difference of course, for brevity I am using a bit of short hand here in words.

 

SQR takes time to computate. So it is better to compare INTEGER numbers by the SQUARE only. It does the same thing so drop the extra square root calculation.

 

So makes program run faster to compare INT D^2 < (x^2+y^2)

 

Next, I thought, maybe faster to do a INT BOX (X,Y) click area. Simply is X and Y between the Box co-ordinates integer (only a <> operation). Maybe that is faster than multiplication?

 

[(X>X1 AND XY1 AND Y

 

Inside the first evaluation, if the X is not in range (say 50% chance same as Y ) using the complied IF statement and register comparison >, then the program will skip the rest of the calculation making it speedy. Which is faster than adding (D*D) < (X*X + Y*Y) to see if it the whole expression evaluates to be TRUE.

 

Is this logic true? I measure the main loop speed and it is much faster using INTEGER and this way to find Distance closer to Mouse Click (x,y)

 

When doing 3D calculations, I think I can use a few math cheat aproximations to speed things up alot there too.

Link to comment
Share on other sites

About partial evaluation of conditional expressions: keep in mind that CPU is highly parallel beast and tends to calculate lot of stuff in advance, which is problematic with too many dynamic branches, that might actually force CPU to throw away already calculated data and thus slow it down. This is a fine example where premature optimization might actually hurt your program's performance.

 

@David: Plan to actually make your source public sometimes?

-rebellion2 enthusiast-

Terra Reconstructed

Link to comment
Share on other sites

  • 3 months later...

My typed message got hosed.

 

steel code using those libraries get 157 FPS, I only get 60 using DB.? The binary functions or the compiler is better under MS version.

 

I copy paste 30 sector code, instead of proper FOR 1 to SECTOR_NUMBER.

 

Now the DB compile EXE is back up to 140 FPS open loop (fast it will go).

 

1) MS compiler is that much better.

2) DB compiler under BASIC is just slow and quirky.

 

3) DB Pro using C+ like language compiled with MS free VC Express 2008 compiler produces 10x faster binary EXE.

 

I guess I should not diss MS so much. It seems to be faster their (MS) compiler logic to making High Level Language into a Binary.

 

Using DB compiler, it is better to write sloppy code to run faster?? Copy paste 1 to 30 code instead of using FOR 1 to SECTOR Next statement runs faster---?.

 

MS compiler and pre-written binary language still blows DB away. I think DB is really only for begineer BASIC and simple games. Real games have to be written in modern language, library function, DX9 etc. and MS very good compiler in the end.

 

I am sorry for being tired wriitng this update after 12 hours messing around. I am trying to figure this out. Please excuse my brevity since my original post got deleted by accident.

 

There is a reason why a person must really use the good MS compiler to make C+ or BASIC into Binary EXE.

 

DB Pro does do C+ plus so I am still going to try that route in vain. DB Pro C+ is faster using VC Express Compiler to some half way speed satisifaction before I give up and go with a real language and a real compiler and a real engine.

 

I will try a bit more.

Link to comment
Share on other sites

  • SWR Staff - Executive
Considering Microsoft is a software company making $60 Billion... I'm not surprised their compiler is better. They get tons of money and support from software developers, so they need to have a highly optimized compiler.

Evaders99

http://swrebellion.com/images/banners/rebellionbanner02or6.gif Webmaster

http://swrebellion.com/images/banners/swcicuserbar.png Administrator

 

Fighting is terrible, but not as terrible as losing the will to fight.

- SW:Rebellion Network - Evaders Squadron Coding -

The cake is a lie.

Link to comment
Share on other sites

Yes indeed. I would like to try another path for this code work. Regular DB pro is OK as long as you are doing a simple game. It falls out once things get "complicated".

 

I would like to give this Dark GDK a try before I am too far into this project. It is sanctioned by Microsoft. It uses the Visual C++ 2008 Express compiler. There is a free download to try it out. It is more C++ language programming looking. Look at this link. It seems to be ALOT faster than the regular Dark Basic Professional compiler.

 

http://gdk.thegamecreators.com/?f=darksdkvsdbpro

 

I think this may solve my problem without having to lose all my code I created so far. I want to run a demo test for you people to try out. It will be the 2D interface and a 3D battle engine to show.

 

This way people can use that free Microsoft Visual C++ 2008 Express compiler, and program in a language more familiar to regular programmers. It should give a huge boost in code speed.

 

`	DarkBASIC Professional Source Code
sync on
sync rate 0

make object cube 1, 10

do
  rotate object 1, object angle x ( 1 ) + 0.1, object angle y ( 1 ) + 0.1, object angle z ( 1 ) + 0.1

  for a = 1 to 100
     for b = 1 to 100
        for c = 1 to 100
           d = rnd ( 100 )
        next c
     next b
  next a

  set cursor 0, 0
  print "fps = " + str$( screen fps ( ) )

  sync
loop

After compiling and running this program in DarkBASIC Professional the frame rate was displayed as 13 frames per second. Here is the same program was created using the Dark GDK. The source code is as follows:

void DarkGDK ( void )
{
dbSyncOn   ( );
dbSyncRate ( 0 );

dbMakeObjectCube ( 1, 10 );

while ( !dbEscapeKey () )
{
	dbRotateObject ( 
			1,
			dbObjectAngleX ( 1 ) + 0.1f,
			dbObjectAngleY ( 1 ) + 0.1f,
			dbObjectAngleZ ( 1 ) + 0.1f 
		);

	for (int a = 0; a < 100; a++)
	{
		for (int b = 0; b < 100; b++)
		{
			for (int c = 0; c < 100; c++)
			{
				int d = dbRnd ( 100 );
			}
		}
	}

	char szFPS [ 256 ] = "";
	strcpy ( szFPS, "fps = " );
	strcat ( szFPS, dbStr ( dbScreenFPS ( ) ) );
	dbText ( 
		dbScreenWidth ( ) - 20 - dbTextWidth ( szFPS ), 
		dbScreenHeight ( ) - 40,
		szFPS
	);

	dbSync ( );
}
}

 

When running the program the frame rate stayed at a constant 27 frames per second which demonstrates a major improvement over the original DarkBASIC Professional version - that is with a straight DarkBASIC Professional to Dark GDK function for function replacement, but by modifying the original code we see other improvements when using the Dark GDK:

 

Another test used the same code but this removed the line "d = rnd ( 100 )" from the DarkBASIC Professional program and "int d = dbRnd ( 100 );" from the Dark GDK program. The frame rate of the DarkBASIC Professional program was displayed as 52 frames per second while the Dark GDK program raced ahead at 2995 frames per second.

 

In a further test the code inside the loop was changed to "d = a * b * c" in the DarkBASIC Professional program and "int d = a * b * c" in the Dark GDK. The DarkBASIC Professional program ran at 38 frames per second while the Dark GDK executable ran at 2988 frames per second. Substantial increases like this are very common when using the Dark GDK due to the core functions being handled directly by the C++ compiler.

 

Link to comment
Share on other sites

Wow. Nice work. DIdnt realise there was such a difference between GDK and DBPro.

 

If its not a problem, would i bet permitted to see the source code Slocket? I am not really stuck but I would like to see how you go about certain things such as events, missions, and general universe set up.

Link to comment
Share on other sites

I am not that far for making events and missions. That is the easy part. What I did is make the hard part that is CPU / graphics intensive to shake down the engine. Sadly, the Dark Basic original engine is limited. It is not a bad engine for what it does, you can make a quick demo or prototype an idea quickly. But a person loses alot of power, so to speak, for that easy to write code.

 

The more the eninge does for you, in this case DB pro allows a begineer like me to do things only a full fledged computer engineer can know. But there is a huge price in performance. It cannot be too complicated or it falls flat on its face for FPS.

 

I hope to re-write using the C like language of GDK for use with Microsoft's Compiler. I realy hope that does the trick. Then I can compare what I have in DB pro compared to that and see if this thing is going to fly.

 

Else, you will see me studying a real language and a real engine in order to make a real professional game.

Link to comment
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
 Share

Copyright (c) 1999-2022 by SWRebellion Community - All logos and trademarks in this site are property of their respective owner. The comments are property of their posters. Star Wars(TM) is a registered trademark of LucasFilm, Ltd. We are not affiliated with LucasFilm or Walt Disney. This is a fan site and online gaming community (non-profit). Powered by Invision Community

×
×
  • Create New...