PHP Classes

Don't you think that using common PHP-Functions on a string l...

Recommend this page to a friend!

      TString  >  All threads  >  Don't you think that using common...  >  (Un) Subscribe thread alerts  
Subject:Don't you think that using common...
Summary:Package rating comment
Messages:7
Author:Ralf Mike Pretzlaw
Date:2009-04-14 10:27:06
Update:2009-04-15 05:59:57
 

Ralf Mike Pretzlaw rated this package as follows:

Utility: Not sure
Consistency: Sufficient

  1. Don't you think that using common...   Reply   Report abuse  
Picture of Ralf Mike Pretzlaw Ralf Mike Pretzlaw - 2009-04-14 10:27:06
Don't you think that using common PHP-Functions on a string like an object in JAVA costs very much performance?
Even creating an instance for using only two or three of the functions takes more time than the usual way.

  2. Re: Don't you think that using common...   Reply   Report abuse  
Picture of Tom Schaefer Tom Schaefer - 2009-04-14 11:53:35 - In reply to message 1 from Ralf Mike Pretzlaw
Indeed. But TString is part of the TypeSafeStruct v2. You' ll see there a practical use.

  3. Re: Don't you think that using common...   Reply   Report abuse  
Picture of Tom Schaefer Tom Schaefer - 2009-04-14 15:25:42 - In reply to message 2 from Tom Schaefer
A Class extending TypeSafeStruct is a replacement for stdClass when using mysql_fetch_object. When the resource record data is set into the TypeSafeStruct object no ValueType object is instantiated (Not much slower, then stdClass).

When you call the data to come out of the TypeSafeStruct, then the value is casted dynamically by its assigned value type object (TEnum, TNumber, etc.).

The casting is normally done when the data outputs starts.
Once casted you can manipulate the value by using the value type methods.

It can be written in fluent design.


  4. Re: Don't you think that using common...   Reply   Report abuse  
Picture of Tom Schaefer Tom Schaefer - 2009-04-14 18:42:41 - In reply to message 1 from Ralf Mike Pretzlaw
I tested it by selecting data from a table with more than 1.000.000 lines.
Looping 100 full select queries with following offsets =>

I.
100 full select queries
mode 0.045 s;

$result = mysql_query("SELECT * FROM ip_group_city LIMIT ".(10000+$i*10).", 10;");
(shifts increasingly)

II.
100 full select queries (using stdclass and typesafestruct)
both mode 0.4s;
$result = mysql_query("SELECT * FROM ip_group_city LIMIT ".(rand(1000,1000000)+$i*10).", 10;");

(jumps randomly between hi and lo)



Some test results:

mysql_fetch_object with stdClass
SELECT * FROM ip_group_city LIMIT 867260, 10;
12633413|CA, Ottawa
12633414|CA,
12633415|CA, Ottawa
12633416|CA, Ottawa
12633424|CA,
12633425|CA, Ottawa
12633426|CA, Ottawa
12633427|CA,
12633431|CA, Vancouver
12633432|CA, Ottawa
0.53570

mysql_fetch_object with stdClass
SELECT * FROM ip_group_city LIMIT 867160, 10;
|PL, Krak¾w
|US, Cockeysville
|US, Shrewsbury
|IT, Bari
|US, Hastings
|AU,
|US, Kansas City
|GB, Kent
|US, New York
|SE, Stockholm
0.57325

---
mysql_fetch_object with TypeSafeStructClass
SELECT * FROM ip_group_city LIMIT 867160, 10;
|PL, Krak¾w
|US, Cockeysville
|US, Shrewsbury
|IT, Bari
|US, Hastings
|AU,
|US, Kansas City
|GB, Kent
|US, New York
|SE, Stockholm
0.57325



-----
// TEST

include_once("TBit.class.php");
include_once("TEnum.class.php");
include_once("TLob.class.php");
include_once("TNumber.class.php");
include_once("TString.class.php");
include_once("TTimestamp.class.php");
include_once("TypedStruct.class.php");
include_once("TypeSafeStruct.class.php");

class Model_IPC extends TypeSafeStruct {

private $int_IpStart;
private $varchar_CountryCode;
private $varchar_RegionCode;
private $varchar_City;
private $varchar_Zipcode;
private $float_Latitude;
private $float_Longitude;

public function __set($key,$value) {
$this->hasProperty($key);
$type = $this->getPropertyType($key);
if($type) {
$this->{$type."_".self::ucfirstAndCamelcased($key)} = $value;
} else {
$this->{"set".$key}($value);
}
}

public function __get($key) {
return $this->{$key};
}

}

$run=1;
$rand = array();
$start = microtime(true);
for($i=0;$i<$run;$i++){
$r = (rand(1000,1000000) + $i*10);
$rand[] = $r;

$link = mysql_connect('localhost', 'root', 'scafa');
if (!$link) die('no connection established: ' . mysql_error());

$db = mysql_select_db('tete', $link);
if (!$db)die ('no able to use db: ' . mysql_error());
echo "SELECT * FROM ip_group_city LIMIT ".($rand[$i]).", 10;\n";
$result = mysql_query("SELECT * FROM ip_group_city LIMIT ".$rand[$i].", 10;");
if(!$result) die("no result set: ". mysql_error());

// begin view operations
while($row=mysql_fetch_object($result, "stdClass")){
echo $row->ip_start . "|" . $row->country_code.", ". $row->city. "\n";
}
mysql_close($link);


}

$end = microtime(true);
echo "\n".number_format($end - $start, 5)."\n";

$start = microtime(true);
// begin data operations

for($i=0;$i<$run;$i++){

// begin data operations
$link = mysql_connect('localhost', 'root', 'scafa');
if (!$link) die('no connection established: ' . mysql_error());
$db = mysql_select_db('tete', $link);
if (!$db)die ('no able to use db: ' . mysql_error());
echo "SELECT * FROM ip_group_city LIMIT ".($rand[$i]-100).", 10;\n";
$result = mysql_query("SELECT * FROM ip_group_city LIMIT ".($rand[$i]-100).", 10;");
if(!$result) die("no result set: ". mysql_error());

// begin view operations
while($row=mysql_fetch_object($result, "Model_IPC")){
echo $row->ip_start() . "|" . $row->getCountryCode().", ". $row->getCity(). "\n";
}
mysql_close($link);
}


$end = microtime(true);
echo "\n".number_format($end - $start, 5);

  5. Re: Don't you think that using common...   Reply   Report abuse  
Picture of Tom Schaefer Tom Schaefer - 2009-04-14 18:46:51 - In reply to message 4 from Tom Schaefer
Correcting typo errors in test results from Message above:

stdclass:
SELECT * FROM ip_group_city LIMIT 223921, 10;
4466700|US, Flushing
4466702|US, Grand Rapids
4466703|US, Clarkston
4466704|US, Taylor
4466712|US, Grand Rapids
4466714|US, West Bloomfield
4466715|US, Troy
4466716|US, Saint Clair Shores
4466718|US, Bloomfield Hills
4466719|US, Lansing
0.16630

Model_IPC:
SELECT * FROM ip_group_city LIMIT 223821, 10;
|US, Toms River
|US, Toms River
|US, Brick
|US, New Castle
|US, Toms River
|US, Toms River
|US, Browns Mills
|US, Willow Grove
|US, Huntingdon Valley
|US, Willow Grove
0.15738


You'll see there is not the big difference!

  6. Re: Don't you think that using common...   Reply   Report abuse  
Picture of Ralf Mike Pretzlaw Ralf Mike Pretzlaw - 2009-04-14 19:18:57 - In reply to message 5 from Tom Schaefer
Is the overhead and parsing-time also in the last time data?
Then it's a small difference.
Otherwise there is more.

  7. Re: Don't you think that using common...   Reply   Report abuse  
Picture of Tom Schaefer Tom Schaefer - 2009-04-15 05:59:57 - In reply to message 6 from Ralf Mike Pretzlaw
Your right! Spaghetti is faster than Ravioli or Lasagne in the first sight line. Regards!