1

I have a very big array stored in memory after reading a whole file in an array (as hex) like this :

$bin_content = fread(fopen($filename,"r"),filesize($filename));
$hex_decode = explode(" ",chunk_split(bin2hex($bin_content),2," "));
unset($bin_content);

function myfunction($i) {
  global $hex_decode;
  // stuff here 
}

When I create a function that uses this $hex_decode array as global ... the script runs like forever (very slow) but if i call that function passing the $hex_decode as a parameter ( myfunction($i,$hex_decode) instead of myfunction($i) in which $i is a pointer) , things are more faster .

Can anyone explain why ? And is there any way to speed the script by reading that file in a different method .

I need to have the whole file in that array rather that line by line , because I'm building a custom ASN.1 decoder and i need to have it all .

4
  • Yet another reason to avoid globals. Commented Feb 8, 2012 at 8:16
  • If you're going to read a file all at once, use the functions that's made for it: file_get_contents. Commented Feb 8, 2012 at 8:22
  • so there is a problem with globals in php ? thanks for the answers Commented Feb 8, 2012 at 8:27
  • Although the biggest problem here is the global var, why not file_get_contents()? Why converting the binary data to formatted hex on read? Commented Feb 8, 2012 at 12:59

3 Answers 3

2

And is there any way to speed the script by reading that file in a different method .

Personally I'd use a stream filter to chunk-read and convert the file as it was read rather than reading the entire file in one go, and then converting and fixing with the entire file in memory, handling any filtering and fixing of the ASN.1 structure within the stream filter.

I know this isn't a direct response to the actual question, but rather to the single quote above; but it could provide a less memory-hungry alternative.

Sign up to request clarification or add additional context in comments.

2 Comments

sounds interesting and complicated :|
It isn't as complicated as it sounds, once you've wrapped your head round the ideas... though the docs are sadly lacking in examples for anything more complicated than changing the case of text in files via a stream filter... I suspect that lack of good code examples makes it feel like a "black art".
1

There already was a similar question at StackOverflow, please take a look at:

The advantage / disadvantage between global variables and function parameters in PHP?

2 Comments

I checked stackoverflow for similar question and did not found it. sorry if I asked this twice. Thanks for the tip .
No problem, two can find more then one!
1

If your files can be huge you could consider a memory-conservative approach. ASN.1 is usually encoded in structures of type-length-value, which means that you don't have to store the whole thing in memory, just the data that you need to process at any given time.

1 Comment

yes .. but the cisco equipment sometimes gives wrong length bytes for some records and i have to have it all in memory to do length checking forth and back . If the ASN.1 would be always correct then I would have done it like you said .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.