Jump to content
MakeWebGames

Tabs or no? Is there a speed difference?


a_bertrand

Recommended Posts

Just to show what kind of difference there is between 2 scripts, one with tabs the other without, beside that the code is EXACTLY the same. Actually I went so far that I removed all the returns as well.

Code 1:

<?php
// Some comment
echo "This is a test";
/***********************************************************
Some multi line comment.
***********************************************************/
for($i=0;$i < 10000;$i++)
{
// this block will do something
$a=sin($i/100.0);
$b=rand(0,1000);
$c=$a*$b;
$d=$a^0xFFFFFF;
$a=doNothing($a);
}

//Now let's create a function
function doNothing($a)
{
// Let's add one to a
$a=$a+1;
// Now let's multiply it by itself
$a=$a*$a;
// Square root... we have again $a+1
$a=sqrt($a);
// Let's remove 1... and we have $a
$a=$a-1;
// Do the same inside an if...
if(true)
{
	// Oooo yeah!
	$a=$a*$a;
	// Why not?
	$a=sqrt($a);		
}
return $a;
}
?>

 

Code 2:

<?php echo "This is a test";for($i=0;$i < 10000;$i++){$a=sin($i/100.0);$b=rand(0,1000);$c=$a*$b;$d=$a^0xFFFFFF;$a=doNothing($a);} function doNothing($a){$a=$a+1;$a=$a*$a;$a=sqrt($a);$a=$a-1;if(true){$a=$a*$a;$a=sqrt($a);}return $a;}?>

 

Now to test it without bias, I made a little bash script which call this 1000 times:

#!/bin/sh
for i in {0..1000}
do
       php -q tabs.php > /dev/null
done

 

And guess the results?

BOTH took 36 sec to run:

real 0m36.025s

real 0m35.905s

...

Times varies a bit if I run the script multiple time as the server is not used purely for that...

Now for the size:

tabs.php: 737 bytes

notabs.php: 235 bytes

Sure you do save quiet a bit of space... but is the space all that important on your server? Beside that honestly I saw no real difference, and therefore I cannot support people which do not indent correctly their script. I said CORRECTLY that means not just add random tabs just to look better, but to really make your code easier to read.

BTW tabs in the PHP page are not sent to the browser, unless you put then inside some echo or outside the PHP tags, therefore you will certainly not add network load either.

To note that all those test have been done on a linux machine without PHP accelerators.

Link to comment
Share on other sites

Yes and next time I ran it the script with tab took even less time, as said the little variations are due to the server load. Sorry Danny696 you are completely wrong here as it has a so small impact that you will not see it even with 1000 calls like I'm doing (and this is to force PHP to re-parse the script each time).

So again, removing spaces and tabs is purely stupidity.

Link to comment
Share on other sites

Honestly it doesn't cost even 2sec to put the tabs correctly if you use an editor which support it. Some use Komodo or InteliJ or Eclipse or NetBeans whatever all those will put the tabs for you while you edit your code. So really there is only to gain while writing correctly a code instead of making a huge mess.

Link to comment
Share on other sites

I always tab and indent my code. It helps me so much when trying to debug anything.

@Paul - I also tab my tables. It makes it much easier to see the text/info/data on each row and in each cell. And I, too, use the same indent size in each of my scripts.

I use Quanta Plus on my Ubuntu laptop, and Notepad++ on my Windows one. Used to use Crimson Editor, but the formatting started to frustrate me.

Thanks for this post a_bertrand. :)

Link to comment
Share on other sites

Honestly I'd prefer readability rather than speed, not that it's a big speed difference.

If you want un-readable code, that's your choice, but if your showing that code, whether it be in the form of looking for help with it, or giving your application away, make it readable! :D

Link to comment
Share on other sites

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.   Paste as plain text instead

  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...