How Print Statements Are Ruining Your Code - Logging's One True Advantage

in #life7 years ago

green-tree-python-morelia-viridis-snake-python.jpg
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.

Want to know why they used the same term for both? Take it up with whoever decided Java and JavaScript are basically unrelated -.-

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.

1.)
while x < 100000:
print 'Kevin Was Here'
x = x + 1

resulting run time: 11 minutes 49.9 seconds

2.)
while x < 100000:
logger.info('Kevin Was Here' )
x = x + 1

resulting run time: 7.6 seconds

programming-language-programs-programming-code.jpg

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!

Cheers!

-Kevin Manton

Sort:  

I am not a fan of logging tbh. That said I am working on some huge web services and we have logging in entry and exit of requests, using the second and correct solution :)

When working on existing code I just rip out print style statements. My view on them is sloppy programmers not cleaning up mess they have worked in some code.

I am strange when it comes to debugging, I run code in my head while I read it and unit test everything. I run a debugger only when I really need to, unit tests are my debug :)

Interesting diagnosis.
So how do us mortals get rid of those time sucking pesky print statements that slow us down so much like a house mover going up a winding mountain road right in front of us?

If you are not the developer of the software...I suggest a magic wand. However, if you do have access to the source code, ctrl + f is your best friend, use it to find the statements and figure out how to comment them out in the language being used :) hope that helps!

So as a mortal I need a magic wand to defend against the evil print spells of unaware software coders.

I hope many coders learn about this helpful advice from you.

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.033
BTC 64222.08
ETH 3135.29
USDT 1.00
SBD 3.99