Jump to content

  • Log In with Google      Sign In   
  • Create Account

making a program, all code in one file


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 lomateron   Members   -  Reputation: 363

Like
0Likes
Like

Posted 28 November 2013 - 12:11 AM

Lets say I have made a c++ windows application and I have all the code in just one .cpp file

with no use of classes, that means lots of global variables and functions

 

What are the good things and bad things about this, in performance?


Edited by lomateron, 28 November 2013 - 02:06 AM.


Sponsor:

#2 Hodgman   Moderators   -  Reputation: 31805

Like
7Likes
Like

Posted 28 November 2013 - 12:58 AM

There's no performance difference at runtime.

 

 

Pro:

When doing a full-rebuild of the project, overall compile times will be better (a practice called a "unitybuild").

 

Cons:

Overall code readability/maintainability will be worse ("oh, that function is on line 103254!").

When making a small change, iterative compile-times will be worse (instead of just recompiling a small part of the project, everything must be recompiled).



#3 Nypyren   Crossbones+   -  Reputation: 4797

Like
1Likes
Like

Posted 28 November 2013 - 01:01 AM

Good things:

- Marginally faster compile and link times compared to normal approaches (much faster compared to non-PCH approaches).

Bad things:

- Visual Studio Intellisense and syntax highlighting will start becoming painfully slow (potentially true for other IDEs depending on how they implement their equivalents).
- The performance of humans maintaining the code will suffer.
- Diff tools and some source control systems will start becoming much slower eventually when dealing with the file as well (though this typically has to be over a hundred thousand lines before you'll care).

Neutral things:

- Runtime performance will not change at all.

(edit) damn, ninja'd

Edited by Nypyren, 28 November 2013 - 01:05 AM.


#4 lomateron   Members   -  Reputation: 363

Like
0Likes
Like

Posted 28 November 2013 - 02:02 AM

what about only one file and no use of classes, that means lots of global variables and functions


Edited by lomateron, 28 November 2013 - 02:04 AM.


#5 Nypyren   Crossbones+   -  Reputation: 4797

Like
0Likes
Like

Posted 28 November 2013 - 02:34 AM

You will see almost no difference in compile or runtime performance, but maintainability will become much, much worse the more global variables you have.

Edited by Nypyren, 28 November 2013 - 02:36 AM.


#6 Buster2000   Members   -  Reputation: 1775

Like
0Likes
Like

Posted 28 November 2013 - 02:35 AM


what about only one file and no use of classes, that means lots of global variables and functions

 

Not neccasarilly .  Using a lot of the features of C++11 you could approach your program in a more functional style and try to avoid the use of mutable state .



#7 Nypyren   Crossbones+   -  Reputation: 4797

Like
0Likes
Like

Posted 28 November 2013 - 02:39 AM

Using a lot of the features of C++11 you could approach your program in a more functional style and try to avoid the use of mutable state .


While this is true, given the nature of the questions above I believe functional style might be too complex of a topic to add to the conversation... smile.png

#8 Squared'D   Members   -  Reputation: 2257

Like
1Likes
Like

Posted 28 November 2013 - 08:01 AM

what about only one file and no use of classes, that means lots of global variables and functions


This is an interesting question. I've never thought of it before. As was mentioned you may be able to speed up your compile time, but the code will be harder to maintain.

Also, keeping everything in one file, using classes, and using globals aren't related. You can put everything in one file, not use classes and still not use globals. When it comes to classes, you can right fast code that uses classes and slow code that uses classes. This is in the graphics forum so I'm assuming you want to speed up some graphics code. By the question you seem to just be starting out. If you're looking for ways to speed up your code, i'd look for better ways like analyzing code that gets called multiple times in a frame. Writing clean maintainable code will be more beneficial than trying to make the code faster by putting it in one file or using globals.

BTW, when I want to quickly prototype something I kind a put everything in one file, but even in these cases, I have a prototyping framework that I link with it that's not in the same file and linked in. I put things in one file in these situations because I just want to test out something quickly and one file makes sense. Other than in testing situations or when writing small console programs, the benefits of keeping it in one file don't out weigh the cost.

Learn all about my current projects and watch some of the game development videos that I've made.

Squared Programming Home

New Personal Journal

 


#9 Norman Barrows   Crossbones+   -  Reputation: 2308

Like
3Likes
Like

Posted 28 November 2013 - 09:53 AM

>>Lets say I have made a c++ windows application and I have all the code in just one .cpp file

>>What are the good things and bad things about this, in performance?

 

as stated above, no difference. you get the same basic exe file irregardless of the number of source files you split the code into.

 

 

>>with no use of classes, 

>>What are the good things and bad things about this, in performance?

 
as stated above, there's fast OO code, and slow OO code. by avoiding OO code altogether, you eliminate the possibillty of slow OO code, and other OO related issues like hierarchy hell, constructor / destructor issues, memory leaks, heap fragmentation, memory allocation overhead, unnecessary runtime type checks, other "safer code" things that OO does that games don't require and add a bit of overhead, etc. but don't get me wrong, you can do all these nasty things in non-oo code too. its just different syntax. in the end its the effiicency of the code that's important. it can be achieved, or totally missed, using either syntax. Its important to understand exactly what the syntax does to ensure efficient code.
 
 
 

>>that means lots of global variables and functions

>>What are the good things and bad things about this, in performance?

 

it avoids oo syntax overhead. thats all. i can write the same code as oo or procedural and get it to run at the same speed. the trick is in how you select your objects. basically, you want your OO code to behave just like your procedural stuff, but use the heap instead of the data segment. so each major data structure becomes an object created once at startup. the overhead for creating the object should not be bad compared to the overhead of allocating memory in the data segment at load time. a bit slower perhaps, but you only do it once. from then on, its the same code, but one uses the data segment, and one uses the heap. since both are essetially "static" data structures, that you don't new and release, you have no heap related overhead or other heap issues. stay away from templates and other generic code that does runtime type checks, and thats pretty much it. OO encapsulated procedural code running off the heap instead of the data segment, just as fast as procedural with globals - down to the clock cycle. again the trick is to understand what the OO syntax does, and more importantly what it does that you don't need and should therefore not use it in "critical section" code. in non critical section code, the benefits of OO syntax (if required) definitely outweigh the slight performance hit. Note that i say if required. not all projects require the extensability and reuse and other benefits that OO syntax provides. for a big company with big projects and big teams, such language capabilities are indispensable. for a single small project and a lone developer, they may be of little practical use - IE overkill.

 

 

 

that should answer your questions re: performance.

 

however, as stated above, issues of readability and maintainability can crop up if the code is poorly designed or poorly organized.  i currently split games into 4 modules: game, game library, audio library, and built-in modeler/animation editor.   On my Caveman project the game source file is about 80,000 lines of code.   Intellisense is slow - or non-existent.  I now do my coding in CScript and then run CScript to generate the C++ code, so i don't miss intellisense(less). A release build takes 55 seconds to link.  but technically, that has nothing to do with the number of source files, its just a big game.  inside the game source file, is actually a series of source files one after the other. so everything is organized into nice modules with clean API's, just like objects. they all just appear one after the other in the same file, that's all. good api design is the key to well organized and well designed code, irregardless of syntax. of course it helps that i'm the only coder, so no issues with lack of code familiarity with the developers. even so, i keep my api's well documented, and have source access if there's a question as to the behavior of a function.

 

then of course there's the danger of misusing globals. again this comes down to good api design, good docs, and coders who know how to use the api correctly. almost any code can be misused. where and how the variables it uses are stored makes little difference. globals perhaps suffer more misuse due to their scope. 

 

 

 

 

 

 

 


Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

 

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

 

 


#10 cignox1   Members   -  Reputation: 723

Like
1Likes
Like

Posted 02 December 2013 - 07:33 AM

Which are the advantages of a giant single room apartment over a more traditional one? Sure, you gain some space (no walls), but you loose privacy, rational furnishing, comfort ... biggrin.png

 

Honestly, I don't see any real reason to write a (non trivial) application in a single file (I'm scared by the maintainance nightmare I would eventually address)



#11 Ravyne   GDNet+   -  Reputation: 8155

Like
0Likes
Like

Posted 02 December 2013 - 11:30 AM

Right. Regardless of any supposed benefits that keeping your code in a single file might have--or staying away from classes (in C++)--all will be swamped by the substantial decrease in productivity. What is it, specifically, that you are trying to achieve by making things "simple" in these ways? We might be better able to help if you ask directly how to achieve your goal, rather than asking for us to comment on your own suppositions on how you might get there.

 

If you use all of the tools that C++ offers you judiciously, then the end-result is almost certainly safer, more-robust, and faster than alternatives you would implement yourself, or work-around without. If you are concerned about clarity of codebase, organizing concepts into distinct files tends to be a benefit; if you are concerned about run-time performance, effective use of custom objects and of the standard library yields fast code--there are times you may need to step away to gain that last 10-20% of performance, but you shouldn't step away from the beginning if practical matters concern you. In short, I can't think that this would be a particularly fruitful endeavor.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS