Have you ever opened an app on your phone, or a game on your PC and been frustrated by how slowly things are loading? In today's fast paced society, speed is everything. As software developers, it is our responsibility to keep our applications running as fast as possible. Whether you are a professional code-monkey, or just beginning your journey with the computer sciences, everybody benefits from fast code. Today I will share with you one of the single most effective, yet overlooked, ways to increase your software's performance.
Recently, at work, I was tasked with creating a logging mechanism for one of the applications under development. After some preliminary hacking around to figure out how the application works, I accidentally proved to myself why logging is such an integral part of any application.
As software is being created, developers often include print statements in order to help debug the code. In the comment section of one of my previous posts I was asked "What is debugging"...So let me take a moment to elaborate.
In terms of software, debugging usually refers to one of two things:
First off, many IDE's (Integrated Development Environment - basically like Microsoft Word...but for code) come with something called a debugger. The debugger is a tool that allows you to step through your code, line by line, while keeping track of the program state (things like variables).
The second use is colloquial, and is simply the term used by programmers to denote looking for, and solving, any issues with the software.
Now back to the print statements. Often these print statements are left in the code as they will not be visible to the end user. But let me tell you, they are a silent killer.
On the other hand, logging, is basically the same idea. instead of print statements, you can introduce log statements that write program information to a text file rather than the screen. Now you may not see any real benefit of writing this information to a file, other than the fact it doesn't disappear when the application is closed...And for many programmers is may seem like an unnecessary evil, having to learn a slightly more complicated way to perform the same task (not really complicated, but there is definitely a larger learning curve than print lines).
Having looked over a number of logging tutorials recently, to aide in my current assignment, it seems most logging advocates focus on levels as the biggest selling point. Logging levels allow you to filter what information is written to the file based on importance. Although this can be very useful, I believe the performance benefit is much more noteworthy. Today, while playing around with logging, I decided to run a couple simple tests.
For any of you who are not avid coders, below I have two simple pieces of Python code. Both of the following do essentially the same thing, print "Kevin Was Here" one hundred thousand times. The first prints this to the screen, while the second logs it to a file.
while x < 100000:
print 'Kevin Was Here'
x = x + 1
resulting run time: 11 minutes 49.9 seconds
while x < 100000:
logger.info('Kevin Was Here' )
x = x + 1
resulting run time: 7.6 seconds
Bit of a difference, right?
The print statement took almost 100 times longer to execute than the log statement!
Now this example is a bit extreme, but it goes to show that those simple little print statements may be a bit less 'harmless' than they seem.
I hope you enjoyed the article!
if you disagree, have questions, or feel like running the code yourself and sharing the results post them in the comments below :)
Don't forget to follow @kavemantech on Facebook and Instagram!