Jump to content

  • Log In with Google      Sign In   
  • Create Account

Dragonion

Member Since 15 Aug 2010
Offline Last Active Jul 31 2013 10:30 AM

#5007157 New is SLOW!

Posted by Dragonion on 04 December 2012 - 12:28 PM


However, in referrencing the loop is translated because two external functions (the int constructor and -destructor) are called in each iteration.

The int constructor and destructor are trivial and not present in the compiler's output. The two function calls you see are the memory allocation and deallocation.


True. But for the sake of simplicity I took the liberty of using the words "constructor" and "destructor" as aliases for the entire memory allocation/de-allocation processes they initiate when you use them.


#5007148 New is SLOW!

Posted by Dragonion on 04 December 2012 - 12:09 PM

Hello all.

I just ran the following two functions 100 times each ...


When using MSVC your code is translated like this:
[source lang="plain"]?passing@@YAXH@Z (void __cdecl passing(int)): 00000000: C3 ret?referrencing@@YAXPAH@Z (void __cdecl referrencing(int *)): 00000010: 56 push esi 00000011: BE 40 42 0F 00 mov esi,0F4240h 00000016: 6A 04 push 4 00000018: E8 00 00 00 00 call ??2@YAPAXI@Z 0000001D: 50 push eax 0000001E: E8 00 00 00 00 call ??3@YAXPAX@Z 00000023: 83 C4 08 add esp,8 00000026: 4E dec esi 00000027: 75 ED jne 00000016 00000029: 5E pop esi 0000002A: C3 ret[/source]
As you can see, the first function, passing, is reduced to a single ret instruction making it equivalent to this one:
[source lang="cpp"]void passing(int j ){}[/source]
In other words, through optimization algorithms the comiler "concludes" (notice the qutation marks because we a talking about a piece of software) that the loop is redundant and removes it entirely from the function body. However, in referrencing the loop is translated because two external functions (the int constructor and -destructor) are called in each iteration. And even though it may seem like this could be optimized out of the loop as well, as soon as you invoke an external function things change quite significantly from a complier's "point of view" because the function's implementation isn't part of your code and is stored in binary form in some library (or object) file on your hard-drive.

I hope this answers your question.


#4850650 How much do you use your IDE?

Posted by Dragonion on 18 August 2011 - 02:25 AM

The situation is a bit different when working as part of a team of 20 on a many-million-line project, plus another 20 authors who no longer work there, plus a million lines of middleware and other external libraries mixed in.

Well, I am not working in a team of 20 people, and I do not have source-code from 20 ex-employees, so even though the situation would indeed be different if that had been the case, it's utterly irrelevant.

Being able to quickly explore and comprehend a foreign code-base is a very important feature in the real world.

That's funny because despite the fact that I don't work in a team I am pretty sure I do live in the real world, and reading the documentation for the SDKs and 3rd party tools I use has always eliminated the need to manually explore the source code in order to figure out how it works. As a matter of fact, I would recommend you begin doing the same. (Trust me; it's much faster!)


#4850332 How much do you use your IDE?

Posted by Dragonion on 17 August 2011 - 08:28 AM

I have encountered many disadvantages.

I know what you mean. Some time ago I decided to try out the Visual C++ 2010 IDE and made a simple Hello World program. Then, after having spend half an hour getting the project to compile because it was missing some files ("export/symbol definition files" or something like that), when I looked at the size of the project folder I was honestly shocked -- 50 MEGABYTES for a program writing one line in a console!! It turned out to be some Intelli-thingy that apparently created some very large files, which, after all, can be disabled. But still; waaay to much junk in that folder compared to a single makefile ;)


#4850222 How much do you use your IDE?

Posted by Dragonion on 17 August 2011 - 04:16 AM

Why would you not use an IDE? Are you chuck norris?

Chuck Norris doesn't even need to code; he simply creates a binary beam from his forehead connecting with the computer and makes whatever program he wants instantly appear on the hard-drive ;)


#4849823 How would a professional comment this code?

Posted by Dragonion on 16 August 2011 - 07:00 AM

Even though I don't have any comments in my current project I still use them from time to time, but the are always preceded by one of the following prefixes which also explains why I use them in the first place:

$FIXME: This section contains a [critical | non-critical | harmless | cosmetic] bug.
$WARN: This section may cause [serious | annoying] problems under specific circumstances.
$TEMP (or $TEST): Temporary test code that should be deleted as soon as the experiment is over.
$OPTI: This section is working but could/should be optimized.
$LATER: This section is working but sometime in the future I should look into a better/more elegant solution to solve the problem.

The reason I put a $ before each token is so that I can easily find them in a global file-search and to ensure they can not be confused variable names (and yes; I do know that VC++ allows you to use $ in variable names but I never do that)


#4845598 To goto or not to goto?

Posted by Dragonion on 06 August 2011 - 05:07 PM

Isn't that more or less what exceptions are for?

In addition to what Antheus wrote about exceptions not existing in C, the concept of goto is actually very natural to the processor, and for that very reason they are usually translated into a single jump instruction, if any:

Goto version
int test (int i)
{
	if(i)
    	goto ERROR;

	return 1;

ERROR:
	return 0;
}
?test@@YIHH@Z (int __fastcall test(int)):
00000000: xor 		eax,eax
00000002: test    	ecx,ecx
00000004: sete    	al
00000007: ret

Exception version
int test (int i)
{
	if(i)
    	throw(0);

	return 1;
}
?test@@YIHH@Z (int __fastcall test(int)):
00000000: push    	ecx
00000001: test    	ecx,ecx
00000003: je      	0000001C
00000005: push    	offset __TI1H
0000000A: lea 		eax,[esp+4]
0000000E: push    	eax
0000000F: mov 		dword ptr [esp+8],0
00000017: call    	__CxxThrowException@8
0000001C: mov 		eax,1
00000021: pop 		ecx
00000022: ret



#4843761 Am I Ready?

Posted by Dragonion on 02 August 2011 - 02:49 PM

This is what i have got for # 1 and 3:

using System;

class BasicProject1
{
	static void Main()
	{
    	string name, nameUp, nameBack;

    	Console.Write("Please enter your name: ");

    	name = Console.ReadLine();
    	nameUp = name.ToUpper(); // string.ToUpper returns a copy of the string so I merged the two lines
    	nameBack = name;

    	Console.WriteLine("Your name in capitals is: " + nameUp); // Good! Task #1 complete <img src='http://public.gamedev.net/public/style_emoticons/default/smile.gif' class='bbc_emoticon' alt=':)' />

    	// I am not going to solve #2 for you, but here is a tip:
    	// Console::ReadKey(Boolean) will read the next keystroke by the user, including function keys,
    	// and if you set the Boolean value to "true" it will not output the read character on the screen.

    	Console.Write("Your name backwards is: ");

    	// When you are dealing with strings (that fall into the category "things that may change at run-time")
    	// it is usually a good idea to assign its length to a local variable when accessing each character in turn
    	// before you start the for-loop like this:
    	//
    	// int i,n=nameBack.Length;
    	//
    	// for(i=0;i<n;i++)
    	// {
    	// 	...
    	// }

    	for (int i = 0; i < nameBack.Length; i++)
    	{
        	Console.Write(nameBack[nameBack.Length -i - 1]);
    	}

    	Console.ReadLine();
	}
}



#4843556 [C++] Why .exe contains class/struct names?

Posted by Dragonion on 02 August 2011 - 06:57 AM

Windows EXE files are store in a format called PE/COFF (Portable Executable/Common Object File Format) which you can read about here: http://msdn.microsof...rdware/gg463119

If you specify a module definition file when linking (using the /def:mydeffile.def option) you can insert an EXPORT section in that file in which you can provide an external symbol alias like MyPublicFunctionName=MyInternalFunctionName. However, I have only tried this with DLL files so I don't know what the outcome will be if you are linking an EXE.

Besides that, structs in C/C++ are merely information about how the data is organized, allocated and referenced in memory. You can certainly export global (non-static) variable instances of a structure (usually just stored as a reserved, labeled area in the .data segment in the EXE/DLL/OCX/...), but I am not sure what exactly you mean by "exporting a structure"? As for those peculiar names you mention, let's say you defined a class containing a function:

class MyClass
{
	int MyFunction (int a);
};

int MyClass::MyFunction (int a)
{
	return 42;
}
When this is translated the compiler will generate an internal, unique name to identify this function; in this case ?MyFunction@MyClass@@AAEHH@Z. But even though "MyClass" is part of this function name, it contains no information about how you defined MyClass in your source code :? What information is it you are trying to export? And what would it be used for?


#4842438 Tips or Tricks for learning about programming?

Posted by Dragonion on 30 July 2011 - 12:56 AM

For example how i should learn for myself instead of asking on forums for help all the time

I have heard about these block-shaped "things"containing a lot of "pages" with useful information called Books. Apparently, if you "read" them they will be able to teach you the most amazing things, and some of them will even guide you from start to finish on how to create virtually whatever you desire! I know it sounds like pure magic, but I actually found this wizard-shop online called Amazon from where you can purchase these wonders!


#4841303 Java vs C# (Mono)

Posted by Dragonion on 27 July 2011 - 03:22 PM

For hobby projects the time spent developing is a lot more important than the run-time performance.

That I totally agree with! Hell, the first few games I developed myself in the early 90's were written in a language called AMOS (for the Amiga 500) which was, if anything, even more user-friendly than C#. Hmm ... coming to think about it I may actually have forgotten I was in the beginner's section. Sorry about that.


#4840187 Java vs C# (Mono)

Posted by Dragonion on 25 July 2011 - 02:46 PM

"Although C# applications are intended to be economical with regard to memory and processing power requirements, the language was not intended to compete directly on performance and size with C or assembly language"

~ One of the ECMA design goals for C# ~

"Like C, Java's goals were really around being able to build relatively specific kinds of software, and we were really going for very distributed, very reliable, interacting-with-people kinds of software -- ones where things like reliability mattered a huge amount, things like security mattered a huge amount, and they all mattered enough that there was a willingness to take a performance hit for them"

~ James Gosling, inventor of the Java programming language ~


... and this pretty much sums up why you shouldn't use either for game development =)


#4834348 How do people stand Java?

Posted by Dragonion on 12 July 2011 - 09:35 AM

But really, "C++ sucks" gives you about 2.3 million google hits (only a few thousand are on these forums). No need to hijack the thread.

You have really got to be kidding me! That's your argument? Google gives you 2.3 million hits, and then it's settled!? Well then, let's try "Al Qaeda rules" ... uhh, not good; 28.8 million hits *yiks*. Idiot.


#4834251 Learning C

Posted by Dragonion on 12 July 2011 - 05:44 AM

I already started to learn C++ and have found it very complicated. I have heard that C is a lot simpler and a tiny bit faster.

The first thing you should be aware of is that C++ (with few exceptions) possesses all the syntax that C does. For that reason, virtually everything you can write in C will be translated by a C++ compiler the same way a C compiler would do it. The main difference is that C++ also provides the ability to write object-oriented code (along with something called template (meta-)programming), which makes C++ a significantly more complex language.

As for speed C++ actually has the potential to outperform C (big time, even), provided that 1) you are using a compiler with good optimization capabilities (like Visual C++), and 2) you know what you are doing. The real problem is that if you do not have a thorough understanding of both the language and how your code is translated to binary instructions by the compiler, your intuitive solution to a given problem may likely be more efficient if you write it in C.

But I would use C# but theres no cross platform or DirectX/OpenGL. I do not want to use XNA. I want to make games the right way.
Python and ruby arent really for 3d games...?

There are basically two types of developers; those focused on development time, and those focused on efficiency. Personally I belong to the latter, but if have no problem raising the system requirements for your product instead of using a couple of years to research and develop a performance-oriented framework from scratch, and you don't mind using a combination of a gazillion different scripting technologies to get the job done as fast as possible (which -in the end- yields a much more error-prone solution, btw), then by all means write your game in C#, Java, Python, Flash, HTML or whatever requires the smallest amount of work (this is how many modern games are developed anyway). If, on the other hand, you want your code to be as efficient as possible then start with C combined with a basic understanding of assembly programming, and move on to C++ when you feel ready for it.


#4834056 Tracking Time

Posted by Dragonion on 11 July 2011 - 05:48 PM

Q1) I use QueryPerformanceCounter to sample the current cycle count every frame, and take the difference of that and last frames cycle count to calculate the time delta of the current frame. Ok, but the issue is that eventually the number returned by QueryPerformanceCounter will loop back around to zero and mess stuff up... I'm guessing I just need to have a check that detects such things and prevent it from going through. Is this what I should do?

Without having seen your code I can almost guarantee that you are accidentally only using the lower part of the LARGE_INTEGER passed as an argument to QueryPerformanceCounter. The thing is that on a 3 GHz computer it would take 0xFFFFFFFFFFFFFFFF / 3,000,000,000 = 6148914691 seconds = 195 years for an unsigned LARGE_INTEGER to actually loop back to zero.

Q2) I need to timestamp some things for various reasons and I figure the best way is to timestamp them with the time since the game started... This would involve keeping the cycle count of the first frame around and subtracting the cycle count of the current frame to calculate the 'time'. Is this how you guys would track time? Thanks as usual!

Personally I think using clock cycles to measure frame time sounds a little fanatical. Also, my own experience with performance counters is not particularly good, either, since the timer-system I wrote a while ago using them turned out to be so inaccurate it was downright useless (though this may not be the case on newer computers). Additionally, I think I once read something about these timers being even more inaccurate on laptops when these computers enter "low power mode" or something like that. My own advice would be to use timeGetTime or timeGetSystemTime since both these functions return the time since Windows started in milliseconds as a single 32-bit value (equivalent to a max session time of 1193 hours), which should be sufficiently accurate. And if you do so, be sure to call timeBeginPeriod somewhere in your program prior to using them with the smallest acceptable value (use timeGetDevCaps to find this) to ensure they are as precise as possible. Also remember to add winmm.lib to your library list when you link.




PARTNERS