In this general programming article, I will show you some ways you can use to write faster code. These methods apply to almost any programming language out there, so I will go ahead and use pseudocode to demonstrate them.

### Breaking out of loops early

When you are writing a loop, you should always ask yourself the following question: “Can I shorten the amount of iterations in this loop?”

You won’t *always* be able to answer this question with a “yes”, but sometimes you can!

Take the following example:

*We have an array of 1000 random numbers, and we want to see if the number “123” occurs.*

// Declare variables number_to_find = 123; haystack = array(1000_random_numbers); found = false; // Loop through all items For (i = 0; i<1000; i++) { If (haystack[i] = number_to_find) { found = true; } } // Did we find the item? If (found = true) { print "The number was found" } else { print "The number was not found" }

Now this means that if you find the number *123* after only 10 iterations, the code will still go through 990 more, unnecessary iterations.

Instead what you should be doing is exit out of the loop right after you have found the number, like this:

// Loop through all items For (i = 0; i<1000; i++) { If (haystack[i] = number_to_find) { found = true; exit loop; } }

### Optimize data structures

This step often seems easier than it is, it requires a bit of thinking!

This method can be applied whenever you have a *finite* set. Let’s say for example you want to do monte carlo simulations on a set of 52 playing cards.

You know there are 52 cards, divided into 4 suits. Typically for any small application, you could represent these cards as such:

cards = array( suit => array(card1,card2,card3,...), suit2 => array(card1,card2,card3,...), ... )

However if we are going to do something as intensive as a monte carlo simulation, this won’t do! Each card face (as integer from 1 to 14) will usually take 32 bits.

If you represent the suits as integers, they will also take 32 bits.

In a case like this, where you have a finite set (a deck of cards is *always *52 cards. You should think about how you can make its storage as small as possible. In this case we can use 6 bits to represent every card, instead of 64!

the first two bytes will represent the suit, the other four bytes will be the face.

cards = array( boolean_array(0,0,0,0,0,0), // 2 of spades boolean_array(0,0,0,0,0,1), // 3 of spades boolean_array(0,0,0,0,1,0), // 4 of spades boolean_array(0,0,0,0,1,1), // 5 of spades ... boolean_array(1,0,0,0,0,0), // 2 of diamonds boolean_array(1,0,0,0,0,1), // 3 of diamonds boolean_array(1,0,0,0,1,0), // 4 of diamonds boolean_array(1,0,0,0,1,1), // 5 of diamonds ... )

### Don’t over-use variables

Some times it is better not to assign variables for every little thing.

Someone might for example do things like this:

items = array(0,1,2,3,4,5); itemcount = count(items); print "there are " + itemcount + " items;

The trade-off here is that more variables are better for debugging, but (especially in scripting languages over compiled languages), not using too many variables would be more efficient. We would then re-write the code as such:

items = array(0,1,2,3,4,5); print "there are " + count(items) + " items";