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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recoding PHP scripts in C - much speed difference?

Status
Not open for further replies.

Maccaday

Technical User
Dec 9, 2003
71
GB
(I've also submitted this to the PHP forum.)

Hi,

I'm setting up a project where speed of execution of scripts/functions is very important. I will initially be writing everything in PHP, and will be caching compiled scripts both on the hard disk and in memory to speed up execution. Much of the code will be re-used in lots of places all over the project, so I was wondering if it was worth re-encoding some of the most used functions in C.

I realise that when compiled, most scripts will end up being pure C anyway (unless you've fiddled with the engine, and have added C++ support or something), so I was wondering if there is any point in re-coding. If a function only uses extensions and other functions that are based on C, then would re-coding the function in C make any speed difference to the execution of the function if it is cached in a compiled state anyway? Does this perhaps depend on the function, which could be written more efficiently in C?

Thanks in advance for any responses.
 
Write it, then profile it (like anything else)

Will it make a difference writing in C?
- If you spend a lot of time manipulating files and directories, probably not.
- If you use a lot of functions which are thin wrappers around equivalent C functions (or system calls), probably not.
Even seemingly expensive things like regular expressions probably just decompose into thin wrappers around a C regex library.

Other factors:
1. The time it takes to do the conversion (say many man-weeks) is probably a lot greater than the time saved (say 10 seconds a run). Basically, you can buy an awful lot of runs if you're not careful.
2. The fact that it is probably harder to debug the equivalent C code.
3. The fact that it is probably less secure to write it in C. PHP no doubt protects its strings from buffer overflows for example, and you have no such luxury in standard C.
4. The fact that the C will be a lot harder to maintain.

I know nothing about PHP [wink]

--
 
Thanks.

You've made some useful points.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top