php inline functions

Started by
10 comments, last by CProgrammer 16 years ago
Hi guys, I'm developing a php script and something that struck me is that calling functions in php is really slow. Profiling has resulted in this actually being the bottleneck. Now I guess I could cut back on the amount of functions butthat would destroy the OO design. So is there something like n inline function in C++ for php? This would solve the problem. It would be really sad to have to convert the whole project to C++ because of this. Well if there is no solution oes anyone know of a tool that assists in converting between languages, strip away all those dollar signs for instance... Thanks in advance. -CProgrammer
Advertisement
PHP is an interpreted language, and that in itself means that it is going to be orders of magnitudes slower than equivalent C++ code. This means that any language construct (function calling, etc) must be completely determined at run time, so inlining cannot be done (as it is a compile-time construct).

as for getting rid of the dollar signs, etc, just use regular expressions to do the work. You should be able to write a PHP program to do this.
Thanks for your reply.
I wasnt sure whether inlining would be possible in an interpreted language, well its definitely possible but I suppose its probably to slow to execute. Well I guess its time to start converting, arg.
I really wished there was some program that helps though, sure replacing regular expressions is a start but theres a lot more that could be done. Or if nothing else at least do all the replacements automatically.
Anyone ever converted php to C++, any tips?

-CProgrammer
If function calls are your bottle-neck, there's likely something wrong in your design already.

Is your code littered with accessors?
The problem is image manipulation, having a function to access the alpha channel instead of performing the low level operations each time is the bottleneck. The alpha channel access function calls a function that uses the php imagecolorat. I think this is perfectly reasonable, having to do bit shifts every time I access an alpha value is bad design. This makes a pass over the image about four times slower. The php version isnt fast as it is but this is unnecessary thats why its time to convert I suppose.
Before you move to C++, what are you doing with this script? How slow is it? An example of this slow code?

It seems a waste if there is one small thing you can change that will speed up everything and you can continue using php.
Well simple things like converting an image to another color space is slow mainly because it requires php to call several functions for every pixel (some of which are accessors created by myself). I suppose some threading would also help which is another reason for switching to C++. Most of my projects are C++, Java or C#. Im fairly new to web development which led me to do too much in php instead of moving to a compiled language for the backend. I've learnt my lesson :)
Quote:Original post by CProgrammer
The problem is image manipulation, having a function to access the alpha channel instead of performing the low level operations each time is the bottleneck. The alpha channel access function calls a function that uses the php imagecolorat. I think this is perfectly reasonable, having to do bit shifts every time I access an alpha value is bad design. This makes a pass over the image about four times slower. The php version isnt fast as it is but this is unnecessary thats why its time to convert I suppose.


Unfortunately, calling a function on per-pixel basis is sub-optimal. The only reason this is "tolerated" in C++ is because combination of templates and inlining will usually produce fast code. In C, macros usually took care of such situations.

When manipulating pixels, especially over a full image, performing operations directly on an array is preferable.

What you're doing is equivalent of GetPixel() in similar APIs, which is, de-facto, slowest possible way to manipulate pixels.

If you're after performance, changing language will not help - you need to approach problem differently. Try to work with raw image data. If you need polymorphic behavior, then something like process(imagedata), where process can have multiple implementations will be considerably better, without breaking OO design.

This is about compromise. If there's a performance bottle-neck, then you'll need to stray from puristic OO design a bit.

Quote:I suppose some threading would also help


Far from it, especially not with this design.
Quote:Original post by CProgrammer
Well simple things like converting an image to another color space is slow mainly because it requires php to call several functions for every pixel (some of which are accessors created by myself). I suppose some threading would also help which is another reason for switching to C++. Most of my projects are C++, Java or C#. Im fairly new to web development which led me to do too much in php instead of moving to a compiled language for the backend. I've learnt my lesson :)


I would refactor this out and have the pixel operations inline. This isn't an excuse to do web development in C++. Although you could do your image manipulation in C++ and the rest in php.

Besides, aren't there functions to convert colour space already? Things like gd and imagemagick that are implemented in C.
Thanks for the replys guys, I was not planning on doing everything in C++, I was just thinking of converting the image manipulation parts. The part about GetPixel is a good tip Anteus, thanks. So php is basically not keeping the values in an array under the hood? I figured since the gd library ued by php is decompressing the image there shouldnt be a performance difference to getting a pixel versus accessing an array.
The down side to converting to an array first in php, is that I have to reimplement copy and resize functions. And the part about comprimising OO design bothers me a lot since this completely destroys the OO design of theimage class but I suppose it could be done.
Anteus how would multithreading not help. This way I could make use of multiple cores and with a bit of extra effort maybe get a descent GPU and use CUDA. Multithreading is often times usefull in imaging.

-CProgrammer

This topic is closed to new replies.

Advertisement