10 Tips for Writing Faster and More Efficient Code
Writing efficient code may seem like an impossible task when you’re just starting out with your programming journey, but the truth is that there are several steps you can take to make your code run faster and use fewer resources. You don’t have to be some sort of genius or even have years of experience to achieve this – here are 10 simple tips for writing more efficient code!
1) Use Type Alias
When you need to define your own data type, consider using a type alias. This saves you from having to repeatedly reference more verbose names like String or NSNumber. Using a type alias also helps reduce pollution of existing types, which can cause problems later on. For example, adding an int property on top of NSObject will break a lot of code that uses that class without changing its definition.
2) Use Option Auto or Type Keywords
Most developers have different preferences, but it is important to use Option Auto or Type Keywords. This will help you focus on your current method/function when writing code. Keep Your Methods Short: As I mentioned above, keep your methods short so that you can complete coding without losing focus. Minimize Wasted Characters: Write lean code with minimal wasted characters.
3) Use List Comprehensions
Creating lists of values in Python is fairly straightforward, but writing out each value can get a bit tedious. List comprehensions are a great way to quickly create list structures, particularly when you want to iterate over two or more collections.
4) Sort Your Lists, Use Arrays
When dealing with lists, remember that sorting them will make them far more efficient. While you could loop through your list and perform a comparison on each item, a sort is much faster: not only does it give you an immediate speed boost, but it also makes adding items to or removing items from your list much easier. A quick way to sort an array in Ruby is using Bubble Sort
5) Break Apart Large Functions
Avoid writing functions with more than a few lines of code. These can be hard to understand, making them difficult to maintain and reuse. Smaller functions are easier to test, simpler to reuse in other parts of your program, and easier to read.
6) Avoid if __name__ == '__main__'
The if __name__ == '__main__' technique can help save time, but it’s a dangerous one. It breaks program flow by jumping to an early part of your code. Only use it when you know that your module or program will only be used from a single entry point (and never from any imports). Use exec() : Python provides a built-in function called exec() , which is most commonly used as a quick way to execute code without writing out all of those parentheses.
7) Divide & Conquer with Indirect Call
One of the best ways to speed up your code is to find places where you can use indirect calls. Indirect calls are one of those under-the-hood tricks that can potentially save you big in terms of execution time, but it’s not a technique that most developers are familiar with. If we take a look at Microsoft’s documentation on indirect call, they explain: An indirect call is used when one function needs to make a second function call.
8) Measure, Don't Guess!
The best way to find out how long it takes to execute an individual piece of code is to measure it. There are a variety of methods for doing so—we use low-level hardware counters, but you can also use higher-level tools like Unity’s built-in profiler or even a debugger like MonoDevelop’s Profiler. Just make sure you get good data!
9) Try a Third-Party Module/Library/Toolkit, e.g. Cython, Numba, CFFI (F2PY), PyPy, Nuitka ...
If your problem is NP-hard, there’s probably a library that can make it faster. Even if you don’t care about efficiency in CPU time (or RAM), you might care about speed in development time: some tools can allow you to write much less code to achieve an effect, and thus save more of your precious gray matter.
10) Generators and Coroutines Are Awesome!
You probably already know that Python has an excellent standard library. However, one of its gems is under-discussed: generators. To quote from Python’s documentation : A generator is a special iterator object which produces a sequence of values on demand—this includes both iterators and generators.
Comments
Post a Comment