if(): howe much time consuming it is?

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

Well when I think about it you may be right. If by calling array[n] array is searched one by one until n, then advancing array one at the time like array++ should be faster.
kh_
Posts: 78
Joined: Fri May 19, 2006 4:29 pm

Post by kh_ »

My first idea is probably a bad one as far as speed is concerned. I was looking for a way to not use "if", I forgot the problem was about speed. :oops:
It's possible what I read about using a pointer that way being faster was outdated. It's just something to consider. If you try it remember not to increment the original.
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

No problem :) thanks for advice. I'll se what can I use.
omaremad
Competition winner
Posts: 1027
Joined: Fri Jul 15, 2005 11:30 pm
Location: Cairo,Egypt

Post by omaremad »

Hmms allot of this stuff is a bit complex for my understanding, but since these are tiles i would employ a chess piece approach:

1-ignore tiles behind camera
2-ignore tiles beyond draw distance
3- in collision detect ignore tiles that are a distance > velocity of object*time tick

that way non interactive/ hidden tiles arent even refrence since all the distance calculations are adds rather than the tradtional distance formula

ie all your distances are in tile units so rendering 500 units of draw distance infront of the camera would call where each tile is 100 units
x= camera tile
[x]
[x+1]
[x+2]
[x+3]
[x+4]
[x+5]
"Irrlicht is obese"

If you want modern rendering techniques learn how to make them or go to the engine next door =p
omaremad
Competition winner
Posts: 1027
Joined: Fri Jul 15, 2005 11:30 pm
Location: Cairo,Egypt

Post by omaremad »

yeah so by creating predefined cases the compiler can just unroll your loops to fast secquences.
"Irrlicht is obese"

If you want modern rendering techniques learn how to make them or go to the engine next door =p
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

It is not so easy :)
normaly whole terrain with all tiles is a single mesh. By spliting terrain in to individual tiles you would efectively kill performance of your program.

I tried to split individual tiles in to mesh buffers = same result as spliting mesh.

So what I finaly did is to split terrain in to groups of tiles, each with its own mesh buffer. Number of mesh buffers is limited to 25. I test if mesh buffer is on screen each loop and let render only those which are. Works fine.

There would be possibility to test each tile and refill indice array each loop. I was not triing that so I don't know howe well it would work.
omaremad
Competition winner
Posts: 1027
Joined: Fri Jul 15, 2005 11:30 pm
Location: Cairo,Egypt

Post by omaremad »

Im not sure if this would easily be implemented in irrlicht but indexed vertexbuffers in ogl and dx could be used, each tile can be stored in the vertex ram(sent once) while the tile engine just sends its index to the gpu every cycle which would be one integer instead of (3 pos,2 texcoord*number of textures,4 color,3 normal)
"Irrlicht is obese"

If you want modern rendering techniques learn how to make them or go to the engine next door =p
RapchikProgrammer
Posts: 279
Joined: Fri Dec 24, 2004 6:37 pm

Post by RapchikProgrammer »

I am not in the current discussion as i hav a headache and not in quite the talkative mood, but i just did a bench mark just for fun and saw that a 1million ifs just cheking a bool without any if and doing nothing take <1ms using vc6 compiler! So i think i think you are safe using a lot of ifs!
BTW Heres the way i did the bench mark, it showed me 0ms so its less then 1ms:

Code: Select all

#include <iostream.h>
#include <time.h>
#include <windows.h>
#include <mmsystem.h>
#pragma comment( lib, "winmm.lib")
#define getHighPerformanceTime timeGetTime

#define BOOL_ARRAY_LENGTH 100000

int main()
{
	bool temp[BOOL_ARRAY_LENGTH];

	srand ( time(NULL) );

	for (int i=0; i<BOOL_ARRAY_LENGTH; i++)
		temp[i] = rand()%2;

	long beforeIfTest = getHighPerformanceTime();

	for (i=0; i<BOOL_ARRAY_LENGTH; i++)
	{
		if (temp[i] = true)
		{

		}
	}

	long afterIfTest = getHighPerformanceTime();

	cout << BOOL_ARRAY_LENGTH << " number of ifs take " << (afterIfTest - beforeIfTest) << " ms" << endl;


	return 0;
}
sio2
Competition winner
Posts: 1003
Joined: Thu Sep 21, 2006 5:33 pm
Location: UK

Post by sio2 »

Code: Select all

	for (i=0; i<BOOL_ARRAY_LENGTH; i++)
	{
		if (temp[i] = true)
		{

		}
	}
That loops does nothing. I'd expect the optimising compiler to just ignore it completely, so you're measuring the time of virtually nothing. :P
Luke
Admin
Posts: 449
Joined: Fri Jul 14, 2006 7:55 am
Location: Australia
Contact:

Post by Luke »

Lol, sio2’s right,
a lot of people make that mistake of measuring loops, etc that compiler wipes.

Compilers are pretty good now days, I pretty sure the compiler can even optimise stuff like:

Code: Select all

for (i=0; i<BOOL_ARRAY_LENGTH; i++) 
{
	n++;
}
to just add BOOL_ARRAY_LENGTH to n. (and i)




about this if stuff, to many people think about this low level stuff to early, when you can get the real speed ups from the high level design of the code, I mean why are you looping though 100 x 100 objects in the first place, in most cases you can avoid it (or at least not do the full loop in one update)
Phant0m51
Posts: 106
Joined: Mon Jan 15, 2007 6:07 am

Post by Phant0m51 »

Your bool array is also only 100,000 not 1 million.
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

omaremad >> well I do not intend to rebuild whole Irrlicht engine, I am not that good programer, nor I have enough time for that so I think I will stick with what is at hand :) But thanks anyway :)

RapchikProgrammer >> howe is your head :) hope you feel better ;) thanks for testing, however I am also not sure is such a strayforward approach gives correct result as already mentioned. But your test gave me some idea: Ill try to run my loop with if() and then without ...of course making sure firsth that all pointers are vallid.

Luke >> I am updating each tile from data array everytime camera change position more than one tile. Sure I can awoid it if I would shift tiles instead of mooving them.
RapchikProgrammer
Posts: 279
Joined: Fri Dec 24, 2004 6:37 pm

Post by RapchikProgrammer »

Firstly i have tried both with an n++ and without one and there was no difference in vc6 at least have no idea about other compilers that means vc6 doesnt ignore it! With or without n++ ait showed me 0ms that is <1ms!

And ya, my bad its 100,000! For a million it took me 11ms, but my pc is running a LOT of different programs rit now so it shud take about 10-11ms in my opinion! Even then i dont think its gonna hurt your performance a lot!
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

Ok I dont think I can make it better. Ill stick with If() ...is quit nice and usefull function afther all :)

Thank you all for help.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

if is not a function, it's a statement.
Post Reply