Bad Architecture
Yea.. nice example Chris, maybe next you'll be right!
Submitted by phrax on Tue, 2004-10-19 15:01. Bad ArchitectureThere's this nagging voice in the back of my head that says, "don't make fun of the readers! They anger easy and won't come back". However, I couldn't resist reponding to Chris's post about using arrays to create strings because it's faster than concatenation!
So this is today's WTF:
You do realize that cating strings is slower than manipulating arrays, right?<?php
// meh...
/* -->8-- */
// update the area code
$stSQL .= "$areacode=$codeData, ";
// update the phone num
$stSQL .= "$phonenum='$phoneData' ,";
/* -->8-- */
// yay!
/* -->8-- */
// update the area code
$update_array[] = "$areacode=$codeData";
// update the phone num
$update_array[] = "$phonenum='$phoneData'";
$query = "UPDATE tablename SET " . implode(', ', $update_array) ....;
/* -->8-- */
?>And of course, you should always be checking to see if a variable actually exists, rather than simply assuming it will be there (*cough*$_POST[$areacode]*cough*). I hope this is just an oversight while making an example of poor code.
But from this little shell script we find that string concatenation is more than twice as fast than using arrays.
Run Number #1 1199999 - bigString, Time: 0.5843870639801 1199999 - bigString2, Time: 1.0092749595642 Run Number #2 1199999 - bigString, Time: 0.34683203697205 1199999 - bigString2, Time: 1.0810549259186 Run Number #3 1199999 - bigString, Time: 0.35090112686157 1199999 - bigString2, Time: 1.0080449581146 ...
Well if you look at the shell script code you'll see I do 100,000 iterations. But if you only do like 20 iterations using an array is actually faster, take a look:
239 - bigString, Time: 0.0015630722045898 239 - bigString2, Time: 0.0010149478912354
Only after about 150 iterations does string concatenation become faster than using an array. See:
1799 - bigString, Time: 0.0024600028991699 1799 - bigString2, Time: 0.0025348663330078
So yea he's right, but then again, who the hell cares about those thousandths of a second? At least I'll be right when generating SQL for tables with more than a few hundred columns! ;b
/petty.
I'm smrt.
Submitted by phrax on Sun, 2004-10-17 14:59. Bad ArchitectureI like to laugh at clever programmers that outsmart themselves. An anonymous user sent in the following.
<?php
/* query_fetch - a very smart function.
It executes and fetches sql data, returning either a value, an array,
or a multidimensional array depending on the query results
*/
function query_fetch ($query,$start=1,$max=1000)
{
...
}
?>
The WTF is what query_fetch()
returns. Depending on what comes back from the database it returns a single value (1 row, 1 column), an array (1 row, multiple columns), or a multi-dimensional array (multiple rows and columns). Cool right? A single function that does all that... ohh very smart.
This function is pretty dumb ass because it might return a scalar one time and an array the next. But who the fuck cares that debugging becomes a nightmare, or that I have to write extra code?! It'll make me look smart and nothing like shows smart like adding unnecessary complexity and requirements!
Yea it's a pain in the ass that sometimes a scalar pops out and fucks up the code. The solution? Write a smarter function!
<?php
function fixate($item) {
if (($item)&&(!is_array($item))){ return(array(0=>$item)); }
return($item);
}
?>
From a WTF to an OMG WTF! Fixer functions are so damn wrong! They don't solve the original problem, they add more complexity and they make the original problem worse. They do make you look smarter though and that's what's important! Look how I solved that data ambiguity problem! Aren't I clever?
/sarcasm.