Re: Wierd ass code...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Satyam escribió:


----- Original Message ----- From: "Satyam" <Satyam@xxxxxxxxxxxxx>
To: "Ryan A" <genphp@xxxxxxxxx>; "php php" <php-general@xxxxxxxxxxxxx>
Sent: Saturday, May 13, 2006 9:53 PM
Subject: Re:  Wierd ass code...



----- Original Message ----- From: "Ryan A" <genphp@xxxxxxxxx>
To: "php php" <php-general@xxxxxxxxxxxxx>
Sent: Saturday, May 13, 2006 9:20 PM
Subject:  Wierd ass code...


Hey,
Been reading some other code that I got from the net,
and  have come across some wierd looking code, would
appreciate it if someone could explain it to me:

$hits = array();
$bytes = array();
$blocked = array();
$baps = array();
$bapm = array();

So far so good.... then further down:

// Add to the running totals
@$hits["$username|$subnet"]++;
@$bytes["$username|$subnet"]+=$byte;
@$baps["$username|$subnet|$this_second"]++;
@$bapm["$username|$subnet|$this_minute"]++;

What kind of arrays are the above? I have never seen
nor worked with arrays like them before.

If you can point me to a particular place in the
manual or a few URLs too would be appreciated.

Thanks,
Ryan

I don't think there is nothing special with the arrays, he is just coding some information into strings, with the vertical bar as separator, and using this as the keys to several arrays where he is collecting some statistical information, such as (it would seem to me)
- number of hits by user and subnet
- number of bytes by user and subnet
- number of hits per user and subnet per second
- number of hits per user and subnet per  minute

I would assume that the variables $this_second and $this_minute are absolute numbers counted from a particular time, not seconds from 0 to 60 within the minute. Likewise for the minutes.

I assume that the @ in front is to avoid the error of trying to increment a value that is not yet set.

I guess that this same thing could have been achieved with multidimensional arrays but either concatenating the variables into a single string is faster or some other reason that makes sense for the application, such as being able to sort by keys in just one operation.

Satyam

Of course, another possibility is that the author does not know how to handle multidimensional arrays (just to say it before anyone else points an obvious alternative)

Satyam

Hi!

I don't think the author does not know how to handle multidimensional arrays. I thought the author was more concerned about performance than other issues people like as coding "beautifuly"... Maybe author's approach is weird, but its twice faster in the worst case!!

Sure the author only needs to loop the whole array and doesn't need multi-dimensional approach... 'cause its a lot faster to iterate once the array is filled!!

Look at the test I've done (below) its seems filling multi-dimensional arrays is n/2 faster than single dimension, but then when you need to read the array data, a multi-dimensional approach is always worse than n*6!! so the total time for setting & getting the data is about n*2 in the worse case... Repeating the same operation multiple times, the multi-dimensional approach always gets worse performance ??, but single-dimension stays performing in a linear way. I think that's really interesting!!

Perhaps there are better approaches for iterating over multidimensional arrays, but that's what I got in 5 minutes coding. I'd like to hear your suggestions about if the author code really worth the pain when performance makes sense.

Attached results and code. (tested on PHP 4.3.4)
--------------------------------------------------------------

Testing bidimensional and single dimension array manipulation

Fill style A, bidimensional array: 2,1603
Iteration style A, bidimensional array: 9,9071
Total time style A: 12,0681

Fill style B, single-dimension array: 4,2362
Iteration style B, single-dimension array: 1,5452
Total time style B: 5,7821

Fill style A, bidimensional array: 2,3548
Iteration style A, bidimensional array: 13,1935
Total time style A: 15,5489

Fill style B, single-dimension array: 4,2891
Iteration style B, single-dimension array: 1,5413
Total time style B: 5,8312

Fill style A, bidimensional array: 2,36
Iteration style A, bidimensional array: 17,8439
Total time style A: 20,2046

Fill style B, single-dimension array: 4,2982
Iteration style B, single-dimension array: 1,5554
Total time style B: 5,8544

Fill style A, bidimensional array: 2,3756
Iteration style A, bidimensional array: 17,8817
Total time style A: 20,2579

Fill style B, single-dimension array: 4,3203
Iteration style B, single-dimension array: 1,5332
Total time style B: 5,8543

Fill style A, bidimensional array: 2,3554
Iteration style A, bidimensional array: 19,2986
Total time style A: 21,6547

Fill style B, single-dimension array: 4,3013
Iteration style B, single-dimension array: 1,5508
Total time style B: 5,8528


<?php
function TimeMS() {
   $ms = substr(microtime(),2,4);
   return (time()*10000)+$ms;
}

function Test_a() {
   global $testA;
   for ($a=1;$a<999;$a++) {
       for ($b=1;$b<999;$b++) {
           $testA[$a][$b] = "$a$b";
       }
   }
}

function Test_b() {
   global $testB;
   for ($a=1;$a<999;$a++) {
       for ($b=1;$b<999;$b++) {
           $testB["$a|$b"] = "$a$b";
       }
   }
}

function Test_a2() {
   global $testA;
   do {
       $k=key($testA);
       $v=$testA[$k];
       do {
           $k2=key($v);
           $v2=$v[$k2];
} while(next($v)); } while(next($testA));
}

function Test_b2() {
   global $testB;
   do {
       $k=key($testB);
       $v=$testB[$k];
   } while(next($testB));
}


print "Testing bidimensional and single dimension array manipulation\n\n";
$a = true;
$b = true;

for ($n=1;$n<=5;$n++) {
   if ($a) {
       $testA = array();
       print "Fill style A, bidimensional array: ";
       $start_time = TimeMS();
       Test_a();
       print ((TimeMS() - $start_time)/10000)."\n";
print "Iteration style A, bidimensional array: ";
       $start_time2 = TimeMS();
       Test_a2();
       print ((TimeMS() - $start_time2)/10000)."\n";
print "Total time style A: ".((TimeMS() - $start_time)/10000)."\n\n";
       unset($testA);
   }
   if ($b) {
       $testB = array();
       print "Fill style B, single-dimension array: ";
       $start_time = TimeMS();
       Test_b();
       print ((TimeMS() - $start_time)/10000)."\n";
print "Iteration style B, single-dimension array: ";
       $start_time2 = TimeMS();
       Test_b2();
       print ((TimeMS() - $start_time2)/10000)."\n";
print "Total time style B: ".((TimeMS() - $start_time)/10000)."\n\n";
       unset($testB);
   }
}
?>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux