Home arrow C++ arrow Page 2 - Easy and Efficient Programming for Contests
C++

Easy and Efficient Programming for Contests


Efficient coding ensures that you complete a task in the smallest time interval possible without complicating your life too much. Now, you do not need to work like this for every little bit of code you type. This approach will be priceless, however, whenever time is of the essence, as it is for a programming contest.

Author Info:
By: Gabor Bernat
Rating: 3 stars3 stars3 stars3 stars3 stars / 9
July 07, 2009
TABLE OF CONTENTS:
  1. · Easy and Efficient Programming for Contests
  2. · How to write code
  3. · Macros, arrays and memory
  4. · Exploiting the language's strengths

print this article
SEARCH DEVARTICLES

Easy and Efficient Programming for Contests - How to write code
(Page 2 of 4 )

 

The problem with most beginners is that they do not realize the importance of  clean and visible code. Most people say that this only puts extra effort into the equation and complicates your life for no reason. They will only see how wrong they are when they start to lose competitions for this. The sooner you comprehend this, the better.

One of the crucial things to learn is how to use the language to create code that is both readable and “followable.” By this I mean that you need to write your code so you can instantly tell at every single point what you are doing. Of course, many things can be done in a multitude of ways; however, you want something clean and simple, so in the event of a bug, you can detect it in a short period.

One of the ways to achieve this is to make use of functions. Use them to structure your code in sub-segments according to tasks. Each piece of a function should nevertheless fit inside a screen; therefore, they should be around 30-50 lines long and not longer. The golden rule is that the complexity of the function (to write/ understand) is in inverse proportion with its length.

Prefer, as long as it is an option, solutions that are simpler, and less bugs can occur inside them. Therefore, use C instead of C++ as long as you can. This way you will create a more reliable program that also will turn out to be faster most of the time.

Sure, C++ offers a couple of ways to resolve elegantly some traditional problems; however, these will show their muscles inside larger and more complex programs. When we are talking about contest programming, C will do so many things faster.

I myself programmed a couple of algorithms in C++ just to observe that the college's code in C runs significantly faster. For instance, sorting a vector of ints with std::sort is slower than using an array of ints and sorting it with the qsort.

Additionally, “cin”, “cout” and “cerr” slow down the application significantly. The bottom line is, if you have an option, do not use STL and C++. When you break down your code into smaller functions, you may use the inline keyword so you will not lose speed due to the function call.

This will tell the compiler at compile time to take this and insert 1:1 to where the function is called (if the function is smaller, and this will not make a significant change to the size of the exe), or just place that in cache of the CPU at run time. However, the cache has a limit, so do not declare all of them in this way, because this will mean none of them is inline.

Make sure you use the const and static keywords also. If a parameter for a function is passed down that should not be modified, use the const keyword. This will save you a lot of time for eventual debugging, because something still changed it. It is a good idea to use the uppercase letters for more important variables, so they will draw your attention and make you concentrate on them more.


blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials