Jump to content

Does blank space really matter in performance?


Ivan Ivković

Recommended Posts

Do I really have to care about the amount of spaces/tabs I have in my program?

 

Is my code good? Example:

 



class SportListout{

public $start;
public $db;
private $junk = 'This is junk data. Unable to find it \'s parents. It is recommended that you delete this data.';

function __construct($db){
	$this -> db = $db;
	$this -> setStartingParent();
}

function searchListout($sp){
	$notif = 'There are no search results that match that criteria.';
	if(strlen($sp) > 0){
		if(strlen($sp) < 3){

 

This is my common text format. Is this ok or should I not make spaces in between functions?

Link to comment
Share on other sites

The effect of white space is so infinitesimal that you really need never worry about it!

 

it's better to produce readable code than bunch everything up to save a few thousandths of a millisecond...

 

:) My two pennies...

 

 

Do I really have to care about the amount of spaces/tabs I have in my program?

 

Is my code good? Example:

 



class SportListout{

public $start;
public $db;
private $junk = 'This is junk data. Unable to find it \'s parents. It is recommended that you delete this data.';

function __construct($db){
	$this -> db = $db;
	$this -> setStartingParent();
}

function searchListout($sp){
	$notif = 'There are no search results that match that criteria.';
	if(strlen($sp) > 0){
		if(strlen($sp) < 3){

 

This is my common text format. Is this ok or should I not make spaces in between functions?

Link to comment
Share on other sites

Although I have no definitive proof I would agree. The PHP engine would only parse the file once. So, if you have a loop in your code the spaces/comments in that loop are only interpreted once upon file load and would have no effect when the loop actually runs. Unless there is a ton of code that is being processed I don't think that extra white-space or comments would make any perceptible difference (for the most part). However, when you get into situations with sites with extremely high levels of usage (think facebook/google) then saving even a tenth or one hundredth of a second on a page load can have an impact on the number of servers you deploy.

 

But, if you really want to check it out for your self - test it. If you find you want to go that route you still don't want to "build" your files without white-space and comments. Instead, you would find/build a utility to strip that content out for you. So, you would maintain the original code with the white-space/comments for development purposes. Then, you would run the files through the utility to remove that content before deploying it to your production environment.

 

However, I suspect there are probably other inefficiencies in your code that would have a greater impact in performance than white-space and comments. That's not to knock your code - I'm sure that's the case in my own code even though I consider myself pretty vigilant to trying to make efficient code.

Link to comment
Share on other sites

Comments and white-space in the php source (outside of quoted strings) are tokenized in to T_COMMENT and T_WHITESPACE tokens with the token value being the whole comment text or the specific white-space characters. When the tokenized byte-code is interpreted, these comment/white-space tokens must be still be processed and skipped over.

 

So, comments and unneeded whitespace in the file, will add a very small amount of extra time (microseconds) to read the actual source file from the disk, a very small amount of extra time (microseconds) to parse through the characters in the file when tokening the php code and any HTML outside of php code, and a very small amount of extra time (microseconds) when actually interpreting the tokenized byte-code.

 

Of an interesting note: anything in a .php file that is outside of <?php ?> tags is tokenized as a T_INLINE_HTML token where the literal content is the value after the token and is output as-is when each T_INLINE_HTML token is interpreted.

Link to comment
Share on other sites

However, I suspect there are probably other inefficiencies in your code that would have a greater impact in performance than white-space and comments. That's not to knock your code - I'm sure that's the case in my own code even though I consider myself pretty vigilant to trying to make efficient code.

 

Such as? :) If there's smthn wrong in what you see, please gimme a tip.

 

Comments and white-space in the php source (outside of quoted strings) are tokenized in to T_COMMENT and T_WHITESPACE tokens with the token value being the whole comment text or the specific white-space characters. When the tokenized byte-code is interpreted, these comment/white-space tokens must be still be processed and skipped over.

 

So, comments and unneeded whitespace in the file, will add a very small amount of extra time (microseconds) to read the actual source file from the disk, a very small amount of extra time (microseconds) to parse through the characters in the file when tokening the php code and any HTML outside of php code, and a very small amount of extra time (microseconds) when actually interpreting the tokenized byte-code.

 

Of an interesting note: anything in a .php file that is outside of <?php ?> tags is tokenized as a T_INLINE_HTML token where the literal content is the value after the token and is output as-is when each T_INLINE_HTML token is interpreted.

 

THANKS! So lesser the HTML spaces, but PHP intact?

Link to comment
Share on other sites

Big companys that have thousands of visitors, will use a PHP minify tool on there .php's and save backup's of the old .php's (NOT compressed) for editing purposes.

 

Like Pshyso said, it only depends how big your site is, if you wan tto see performance.

 

If you're running a high volume website, the best bet would backup a copy of your .php (so easy to read) and make a minified version to put it on the live server.  then if u need to edit, just go back re minify when you're done.

 

Problem Solved.

Link to comment
Share on other sites

Ok I'm programming an admin board, so that's not really nessessary then. :) So it's not a lot of extra processing power... But when I start developing large-scale I'll keep the backuping method in mind.

 

lol =], it's really just a personal preference, I really don't even know if you'll see drastic speed increase to be honest, but I do cuz it makes me feel like all my .php's are tight and super clean before i load them to the live server :D

 

a Weird fetish :D

Link to comment
Share on other sites

Like the others, I don't worry too much about white space. Producing code that's easy to scan trumps the minimal performance gains you might get. However, I am more concerned with the amount of space added to the HTML code via PHP. This content needs to be downloaded by the visitor who may have a slow Internet connection. For example, I used to output data tables using the following format:

 

<table>
<tr>
	<th scope="col">Val 1</th>
	<th scope="col">Val 2</th>
	<th scope="col">Val 3</th>
	<th scope="col">Val 4</th>
	<th scope="col">Val 5</th>
	<th scope="col">Val 6</th>
	<th scope="col">Val 7</th>
	<th scope="col">Val 8</th>
	<th scope="col">Val 9</th>
	<th scope="col">Val 10</th>
</tr>
<tr>
	<td>1</td>
	<td>1</td>
	<td>1</td>
	<td>1</td>
	<td>1</td>
	<td>1</td>
	<td>1</td>
	<td>1</td>
	<td>1</td>
	<td>1</td>
</tr>
<tr>
	<td>2</td>
	<td>2</td>
	<td>2</td>
	<td>2</td>
	<td>2</td>
	<td>2</td>
	<td>2</td>
	<td>2</td>
	<td>2</td>
	<td>2</td>
</tr>
<tr>
	<td>3</td>
	<td>3</td>
	<td>3</td>
	<td>3</td>
	<td>3</td>
	<td>3</td>
	<td>3</td>
	<td>3</td>
	<td>3</td>
	<td>3</td>
</tr>
<tr>
	<td>4</td>
	<td>4</td>
	<td>4</td>
	<td>4</td>
	<td>4</td>
	<td>4</td>
	<td>4</td>
	<td>4</td>
	<td>4</td>
	<td>4</td>
</tr>
<tr>
	<td>5</td>
	<td>5</td>
	<td>5</td>
	<td>5</td>
	<td>5</td>
	<td>5</td>
	<td>5</td>
	<td>5</td>
	<td>5</td>
	<td>5</td>
</tr>
</table>

 

 

The above table results in an HTML file around 1.39Kb. But if the table was condensed so that each row was on one line, the file is reduced to around 1.14Kb.

 

<table>
<tr><th scope="col">Val 1</th><th scope="col">Val 2</th><th scope="col">Val 3</th><th scope="col">Val 4</th><th scope="col">Val 5</th><th scope="col">Val 6</th><th scope="col">Val 7</th><th scope="col">Val 8</th><th scope="col">Val 9</th><th scope="col">Val 10</th></tr>
<tr><td>1</td><td>1</td><td>1</td><td>1</td><td>1</td><td>1</td><td>1</td><td>1</td><td>1</td><td>1</td></tr>
<tr><td>2</td><td>2</td><td>2</td><td>2</td><td>2</td><td>2</td><td>2</td><td>2</td><td>2</td><td>2</td></tr>
<tr><td>3</td><td>3</td><td>3</td><td>3</td><td>3</td><td>3</td><td>3</td><td>3</td><td>3</td><td>3</td></tr>
<tr><td>4</td><td>4</td><td>4</td><td>4</td><td>4</td><td>4</td><td>4</td><td>4</td><td>4</td><td>4</td></tr>
<tr><td>5</td><td>5</td><td>5</td><td>5</td><td>5</td><td>5</td><td>5</td><td>5</td><td>5</td><td>5</td></tr>
</table>

 

 

Link to comment
Share on other sites

Generally web pages are served gzipped these days, so the size of the file isn't the amount of data the user is actually downloading. You should concentrate on reducing the size of your images, the amount of external requests you're making, code efficency, etc. If you're still worried about white space, there are plenty of minifying tools out there.

 

Edit: Plus gzip will drastically reduce the size of simple ASCII text, is maintaining code like that worth a fraction of a kb? Certainly not.

Link to comment
Share on other sites

However, I suspect there are probably other inefficiencies in your code that would have a greater impact in performance than white-space and comments. That's not to knock your code - I'm sure that's the case in my own code even though I consider myself pretty vigilant to trying to make efficient code.

 

Such as? :) If there's smthn wrong in what you see, please gimme a tip.

That wasn't an accusation just a statement that anyone will likely gain much more in performance by looking for existing inefficiencies in the logic of their code. Any good developer is constantly learning and implementing new/better processes as they move forward. So, if you were to look at code you wrote 6 months ago you should find things that you would do differently today. The real question is "whether" you should go back and review the code. If it is doing what it is supposed to do and there are no performance problems then the answer is usually no.

 

Plus, if you really need to squeeze out every last bit of efficiency in the code there are always things that we may prefer to write one way but would be more efficient in another. In my case I like using the ternary operator for succinct manner in which I can assign a value to a variable. But, it is less efficient than a longhand if/else statement. But, I will take that infinitesimal hit in performance in order to have code that I feel is easier to analyze/digest.

 

@cyberRobot, doing that to your HTML will them make it extremely difficult to debug any errors produced in your output. Besides just one moderately sized image will normally be larger than all of the HTML code for your HTML file.

Link to comment
Share on other sites

@cyberRobot, doing that to your HTML will them make it extremely difficult to debug any errors produced in your output.

 

I'm not sure what you mean. For me, having an entire table row, for the average data table, is easier to troubleshoot. But I can see this being an issue if each column contains more than just a simple number.

 

 

Besides just one moderately sized image will normally be larger than all of the HTML code for your HTML file.

 

I can see what you mean with being more concerned with image size since that will most likely have a greater impact. My example, comes from back in the days when I converted research reports to HTML. These reports were typically hundredes of pages long with dozens of data tables. When I went from having each column on it's own line to one line per table row, the file size would drop by 100s of kilobytes. Granted, this won't be typically for the average developer out there.

Link to comment
Share on other sites

Granted, this won't be typically for the average developer out there.

 

Exactly, that's a very specific situation. Then again that isn't going to be much of a difference relative to the full file size. Plus you can minify it, and as I mentioned gzip makes short work of ASCII text. I really don't see the point in getting so hung up on white-space, there's bigger fish to fry!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.