typedef struct user {
int ID;
char name[50]; //fixed length
}
fwrite(fp, lpbuffer, sizeof(struct user);
can PHP provide the similar function? Any sugguestion will be appreciated
Moderator: General Moderators
Code: Select all
<?php
# Test a class with no methods
class Struct
{
var $count = 0;
var $initial = B;
}
# Instantiate object
$my_struct = new Struct;
# Echo a value
echo $my_struct->initial;
# Change a value
$my_struct->count = 1;
# Echo the value that was just changed
echo $my_struct->count; echo "\n";
?>Code: Select all
# Remember, $my_struct is the object. Also, I am just echoing the information
# back for the example. One can do whatever they wish between the "{ }"'s.
while(list($key, $data)=each($my_struct))
{ echo "$key = $data"; }Code: Select all
$struct array(
$count => "0",
$initial => "B"
);Code: Select all
<?
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
class Struct {
var $count = 0;
}
$time_start = getmicrotime();
$struct = new Struct;
for($i = 0;$i < 100000; $i++) {
$struct->count = $i;
}
$time_end = getmicrotime();
echo "Using a class took: " . ($time_end - $time_start) . "seconds.<BR>\n";
$time_start = getmicrotime();
$array = array(1 => 0);
for($i = 0;$i < 100000; $i++) {
$arrayї1] = $i;
}
$time_end = getmicrotime();
echo "Using an array took: " . ($time_end - $time_start) . "seconds.<BR>\n";
?>Code: Select all
<?php
class PostPageVars
{
var $var1 = null;
var $var2 = null;
// ..etc..
function setVar($name, $value)
{
$this->$name = $value;
echo 'PostPageVars ' . $name . ' is ' . $this->$name . '<br />';
}
function getVar($name)
{
#/*
if(is_null($this->$name))
{
echo $name . ' is null.<br />';
} else {
echo $name . ' is ' . $this->$name . '.<br />';
}
#*/
return $this->$name;
}
}
?>I wonder if these little changes help a little.Matt Wade wrote:Using a class rather than array seems to run about 10-12% slower in my tests. Here is a simple test script I used:
Code: Select all
<? function getmicrotime(){ list($usec, $sec) = explode(" ",microtime()); return ((float)$usec + (float)$sec); } class Struct { var $count = 0; } $time_start = getmicrotime(); $struct = new Struct; for($i = 0;$i < 100000; $i++) { $struct->count = $i; } $time_end = getmicrotime(); echo "Using a class took: " . ($time_end - $time_start) . "seconds.<BR>\n"; $time_start = getmicrotime(); $array = array(1 => 0); for($i = 0;$i < 100000; $i++) { $array[1] = $i; } $time_end = getmicrotime(); echo "Using an array took: " . ($time_end - $time_start) . "seconds.<BR>\n"; ?>
Code: Select all
<?
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
class Struct {
var $count = 0;
}
$time_start = getmicrotime();
$struct = new Struct;
for($i = 0;$i < 100000; ++$i) {
$struct->count = $i;
}
$time_end = getmicrotime();
echo "Using a class took: " . ($time_end - $time_start) . "seconds.<BR>\n";
$time_start = getmicrotime();
$array = array(1 => 0);
for($i = 0;$i < 100000; ++$i) {
$array['1'] = $i;
}
$time_end = getmicrotime();
echo "Using an array took: " . ($time_end - $time_start) . "seconds.<BR>\n";
?>Code: Select all
$array = array('1' => 0);
for($i = 0;$i < 100000; ++$i) {
$array['1'] = $i;Code: Select all
$array = array('one' => 0);
for($i = 0;$i < 100000; ++$i) {
$array['one'] = $i;Code: Select all
<?php
function dbQuery($mysql)
{
$query = mysql_query($mysql) OR die('Error: ' . mysql_errno() . ' - ' . mysql_error() . '<br />');
return $query;
}
?>