Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Performance question

Status
Not open for further replies.

skiflyer

Programmer
Sep 24, 2002
2,213
US
I'm reading through a file of the form

x=y
a=b
c=d

I always want the value of b

So right now I read through line by line and say if preg_match ("a =", line)
it's my line

So my questions are

a) Better to truncate "a=", or to preg_replace "a=" with ""

b) Or is there just a better way to grab info out of a file like this?

And I realize my first way works fine, I want it fast though.

-Rob
 
If your file will always be of the form you show above, then you can use PHP's parse_ini_file() function ( This returns an array of key=>value pairs, rom which you can extract your value.

Using regular expressions to parse a file like this is probably going to be slower than using a function like parse_ini_file, because that function is written in C.

However, without knowing more about your situation, it is hard to provide any good advice. There are many ways to optimize, and it depends on exactly what you want to optimize: memory usage, disk usage, or processor usage. Also, there are questions about the file itself: what is the typical size of the file you want to parse? Will it grow in size? Is a=b always on the same line of the file, or could it be anywhere? -------------------------------------------

Big Brother: "War is Peace" -- Big Business: "Suspicion is Trust"
(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top