Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does whitespace impact on performance?

This is something I've always wondered about, so here goes.

When writing code, I was/am taught to space out lines, comment them, etc... to improve the readibility (as I guess most of us are). I obviously don't see this as any kind of problem, but it got me thinking, if all of this whitespace and commented sections are being ignored by the compiler/interpreter or whatever else, how much does this impact on the its performance?

Admittedly, I don't know a lot about how a compiler operates - only the basic concepts. However, I have a fair idea that for one to be able to "ignore whitespace", it would first need to identify it (at least), and that takes work, and therefore time.

So then I thought, what about whitespace or comments at extreme levels? Say, millions or billions of sections of them?

I guess the question I'm asking is: At what point (ie. extreme level) will ignored sections of code impact a compiler's/interpreter's ability to produce a timely result and therefore impact on a user's experience?

Thanks.

like image 708
daniel Avatar asked Mar 11 '11 01:03

daniel


People also ask

Does whitespace affect performance?

No. We call that "whitespace" and in most (but not all) languages it is ignored. No matter what, no form of punctuation effects the execution speed of your code.

Does white space slow down processing?

Show activity on this post. In a word, no!

Why is whitespace important?

White space, also known as “negative space,” is empty space around the content and functional elements of a page. The basic role of white space is to let your design breathe by reducing the amount of text and functional elements that users see at once.

How does white space affect program execution?

No, Whitespaces (blank space) in your code are ignored by the compiler, which just skips over them while compiling the code. This also applies to comments. Spaces will only effect the size of your source code.


2 Answers

Try this:

Do comments affect Perl performance?

Edit for comment.

Simple example using a hello world in Scheme with varying zillions of comment lines:

netbsd1# ls -l file* 
-rw-r--r--  1 root  wheel        1061 Mar 11 00:01 file.out
-rw-r--r--  1 root  wheel      102041 Mar 11 00:01 file1.out
-rw-r--r--  1 root  wheel    10200041 Mar 11 00:01 file2.out
-rw-r--r--  1 root  wheel  1020000041 Mar 11 00:03 file3.out
netbsd1# for i in file*
> do
> echo $i
> time ./scm $i
> done
file.out
hello world
    0.06s real     0.01s user     0.01s system
file1.out
hello world
    0.03s real     0.01s user     0.02s system
file2.out
hello world
    0.64s real     0.28s user     0.30s system
file3.out
hello world
   61.36s real    11.78s user    41.10s system
netbsd1# 

Clearly, the 1GB file had major impact which is not necessarily surprising considering I only have 512M of RAM on this box.

Also, this is interpreting/compile speed. If you actually compiled these files, the runtimes would all be identical. You can draw your own conclusions defining impact.

like image 178
Will Hartung Avatar answered Sep 27 '22 19:09

Will Hartung


It will not affect the compiled data as the word implies. However please dont go for comment diarrhea, it will affect other programmers performance.

like image 37
stefan Avatar answered Sep 27 '22 19:09

stefan