开发者

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 开发者_JS百科of code impact a compiler's/interpreter's ability to produce a timely result and therefore impact on a user's experience?

Thanks.


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.


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


It depends.

In compiled languages, compilation could take longer, but you probably don't care since this is done once.

In interpreted languages, there will be wasted load time, execution time and more memory usage if the interpreter keeps the text in memory.

In something like JavaScript being delivered to a browser, you don't only have to worry about parse time, but also transmitting all of those comments to your client's browser. Because of this, many people will run their scripts through a minifier that pulls out comments and does other tricks to reduce code size.

For severely over-commented code, plagued by comments emitted by code-generators, over-zealous revision control systems, and "religious commenters", I would actually worry more about the poor reader / reviewer that has to wade through all of that mostly useless and probably out of synch text to get to the code.


Compiling (and linking) is phase 1.

Execution is phase 2.

Since phase 1 is at least O(input length) you can expect phase 1 to take time proportional (at least) to the input length. If the file length is under 10^4 lines it probably won't bother you too much. If the file length is 10^12 lines it might take years, if something doesn't break first.

But that will not affect phase 2. What affects phase 2 is how much work the program does and how much it needs to do.


If you're talking about a compiled binary then there's exactly 0 impact on performance - they're just a sequence of instructions executed, whitespace doesn't really exist as a concept in this sense. If you're talking about interpreted languages then I guess theoretically millions of lines of whitespace could have a very slight impact on performance, but not enough to ever be noticeable.

In short, whilst an interesting question from an academic viewpoint it's not something you should ever worry about, whether you're using a compiled or interpreted language. Always favour readability and comments. If you quote performance for a reason not to use whitespace or comments, future maintainers of your code will be out to get you!


Whitespace in source files has zero impact on a user's experience. Once the binary is compiled, that's that. It doesn't matter if the compiler took delta-t longer to parse your source code because there were millions of comments.


White space affects performance when whites space compiles into machine instructions. Luckily, most sane languages don't do this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜