javatime-complexitycode-formattingspace-complexity

does spacing between statements, indentation and comments in code affects its time and space complexity?


I'm trying to understand how code formatting and documentation impacts the performance of a program

Spacing between statements: having more or fewer blank lines between code statements. Indentation: The use of tabs or spaces for indentation and whether the level of indentation (nested blocks) has an impact.

I had solved a problem in leetcode using java, every time I submit by making some changes(as mentioned in question) the runtime and memory usage changes.


Solution

  • Java is a compiled language. When it is compiled, all spacing, indenting and comments are discarded. Only code is converted to executable format.

    The issue you are seeing is probably you don't control the execution environment and there may be competing processes. Or it may be that the site uses an interpreter. Even with an interpreter the time parsing those parts is likely small or irrelevant. For sure it doesn't impact the complexity of an algorithm.

    As other mentioned in the comments, you should perform your own testing in a controlled environment to get consistent results.