Jump to content

pigmonkey

Members
  • Posts

    10
  • Joined

  • Last visited

    Never

Everything posted by pigmonkey

  1. I moved both $ar's outside of the memory testing range and changed the second one to $ar2 (so that it wouldn't cause a drop in memory from that being overwritten), and this is what I got as output: 10000 iterations, 545608 bytes in array 10000 iterations, 545608 bytes in array (100.00%) This makes sense, since in both cases it's just storing it as a simple reference until it gets modified. But, this gave me the idea of taking the loop out and just trying $ar and $ar2, and I finally got it, I think. <?php $start = memory_get_usage(); $ar = array_pad(array(), 4, "A"); $end = memory_get_usage(); printf("%d bytes in array\n", $end-$start); $start2 = memory_get_usage(); $ar2 = array("A"); $ar2[] = $ar2[0]; $ar2[] = $ar2[0]; $ar2[] = $ar2[0]; $end2 = memory_get_usage(); printf("%d bytes in array (%.2f%%)\n", $end2-$start2, ($end2-$start2)/($end-$start)*100); ?> Output: 496 bytes in array 496 bytes in array (100.00%) I tried re-implementing this into the loop, and it came out to within 40 bytes of each other, which I will accept as close enough. So, what this means is, when you call array_pad(), it makes the first element unique, and then all the other elements in the padding are just references to that first unique element.
  2. Okay, hold on. I unmarked it solved, since I realized I jumped to a conclusion in my last post. Yes, changing all the strings from "A" to "B" did cause it to go up 35% to be in line with the second test, but changing all the variables from "A" to "B" in the second test causes that to go up a further 35%, which still puts it 35% when the data is the exact same. The numbers were coincidental (or not) and I didn't think to keep testing. The part I don't understand about your original post is: I'm not seeing how you confirmed your suspicion that it was storing references, since in your test case, it was actually storing references, and the memory usage dropped to almost 1/10th of the original.
  3. That certainly makes sense. To further experiment, I discarded the second test and replaced it with this: foreach($arr as $i=>$a){ foreach($a as $j=>$b){ $arr[$i][$j] = "B"; } } So basically, writing over every "A" with a "B". After this, I saw an increase of 34.75%, which is exactly the same increase I saw when I had the original second test. So I guess the bottom line is don't use array_pad() when doing memory benchmarks. Thanks for your help.
  4. As expected, nothing changes. Even if I free up the memory from the first cycle, I'm taking the measurements both before and after, so the value is always relative. It should be noted that if I switch the two tests entirely, the second one comes in at ~75% memory usage of the first (expected, as 1/1.3474 ~= .75). So, I'm fairly sure that I'm benchmarking correctly.
  5. I've been doing some benchmarking for memory usage for a very resource-sensitive project I'm doing (that I probably shouldn't be using PHP for, but oh well). I came across something peculiar, and I'm trying to figure out why I get results as I do. Here is my test file: <?php $string_parts = 4; $max = 10000; $arr = array(); $i = 0; $start = memory_get_usage(); for(;$i<$max;$i++){ $arr[] = array_pad(array(), 4, "A"); } $end = memory_get_usage(); printf("%d iterations, %d bytes in array\n", $max, $end-$start); $arr2 = array(); $i = 0; $start2 = memory_get_usage(); for(;$i<$max;$i++){ $arr2[] = array("A", "A", "A", "A"); } $end2 = memory_get_usage(); printf("%d iterations, %d bytes in array (%.2f%%)\n", $max, $end2-$start2, ($end2-$start2)/($end-$start)*100); ?> Output on my system: 10000 iterations, 4145696 bytes in array 10000 iterations, 5585744 bytes in array (134.74%) A 35% memory gain for two of the same exact arrays seems really weird. Any information as to why, or perhaps any useful functions or resources would be appreciated.
  6. While I don't have access to php.ini, I know that .htaccess is certainly doing something. If I comment out the lines that I posted above, the php script as well as phpinfo() tells me that magic quotes is enabled. Though, I will admit I don't know what "AllowOverride All" does or if it's necessary. I tried putting that in my .htaccess file, but got a 500 error.
  7. I realized today that Magic Quotes were enabled (magic_quotes_gpc=On in phpinfo(), so I needed to turn them off. I added the following to my .htaccess file in the root folder: php_value magic_quotes 0 php_flag magic_quotes off php_value magic_quotes_gpc 0 php_flag magic_quotes_gpc off Yes, probably complete overkill, but oh well. Now, magic_quotes_gpc=Off in phpinfo(), and get_magic_quotes_gpc() returns false like I would expect it to... except things are still getting slashes added to them. Here's my test file: <?php if(get_magic_quotes_gpc()) echo "Magic quotes are enabled<br>"; else echo "Magic quotes are disabled<br>"; echo nl2br(print_r($_POST, true)); ?> <form method="POST"> <input type="text" name="one"> <input type="text" name="two"> <input type="submit" value="Submit"> </form> And with the input: I get the output: So, despite the fact that magic quotes seem to be disabled, things are still getting random slashes. I could just strip them, but until I find the underlying cause, I have no control or idea if it will stop doing this. I'm completely out of ideas, and numerous attempts at searching has led me to nothing. What could be a possible cause here?
  8. I wasn't sure if this should be in the OOP board, but it is a more syntactical error, I believe. Basically, some code I wrote works fine on one server, but fails on another. The original server was running 5.2.5 and the latter is running 4.4.6. The code I'm using to test is very simple: <? class Foo { public static function aStaticMethod() { // ... } } ?> Copied it straight off of php.net. The error I'm getting is So is this a version problem? I searched for quite a bit and couldn't find any limitations in php 4 that would prevent this from working.
  9. Alright, this seems to work. I guess what I will do for the sake of organization is set up a second file with all the functions, and then reference them by name in my original array file that I spoke of. Thanks.
  10. What I'm trying to do is have this array with a bunch of indexes, and each index is an array with some text and values, but I also want each index to have a function of code it can run when I call it. The program will loop through the array and call each bit one by one. I know I can probably use eval(), but I'd rather not, as it is really slow. Is there any other real way I can accomplish this? I know in JavaScript you can set variables as functions (e.g. var test = function() { code; }), but I'm not sure if it's possible in PHP.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.