Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

PHP running exec() Windows program very slow in comparison to UNIX equivalent program

735 views
Skip to first unread message

Otis

unread,
Feb 5, 2008, 5:08:19 AM2/5/08
to
Does anyone have an explanation or solution for the following.

I have a PHP script that uses exec() to call a program to do certain
calculations.

The UNIX version of this program I call runs in about 4 to 5 seconds.
When I run the same PHP script using exec() to call the Windows equivalent
of this same program on my Windows desktop, it takes about 32 seconds.

So the question is, "Why is PHP on UNIX calling a UNIX program through the
use of exec() so much faster than when I have PHP calling a Windows .exe on
a very fast Windows desktop computer? 6 times faster!!

Is there something I need to set in php.ini to get PHP and Windows to work
faster together?

By the way, I run a comparable compiled VB desktop program with no PHP, with
many more iterations than the PHP script uses, and it does the same
calculations and a bunch more in about 3 seconds. So the problem is not with
the .exe file - it is a problem involving both PHP and Windows.

What is it about the interworking of PHP on Windows with Windows that is
really slow?

Thank you.

Otis

Jerry Stuckle

unread,
Feb 5, 2008, 6:32:59 AM2/5/08
to

I'm not clear - is this a compiled program (.exe) on Windows, also?
What language?

No reason in PHP why it should do that. There is very little overhead
in PHP for calling exec() in either Windows or Unix.

Maybe look at why Windows is taking so long? For instance - does it
have to load a bunch of DLL's, are you short of memory or other
environmental considerations?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstu...@attglobal.net
==================

Otis

unread,
Feb 5, 2008, 5:21:01 PM2/5/08
to
>> is this a compiled program (.exe) on Windows, also? What language?

Yes, compiled in C, I believe. Just for fun, I wrote a little program in VB5
that essentially does what this other compiled C Windows program does in
just one of the loops that gets executed about 140 times (at about 38 msec a
loop on Windows). To my astonishment, my little VB5 compiled program, which
is only 22kbytes) took twice as long as the C .exe program to process the
140 loops, 11.5 secs versus 5.5 seocnds. I know for a fact that VB5 and VB6
compiled programs are very fast. My desktop program of the same application
runs lots more calculations, through many more loops, in about 3 seconds
total. So it is the interaction between PHP and the .exe on Windows that is
slow. The same PHP script with a UNIX ".exe" runs in 4 to 5 seconds.

Here is the line of code that runs so differently, time-wise:

exec ("swetest -bj$ejd -p$p_idx -fls", $out);

My PHPEd profiler shows this line of code to be the time hog.


>> No reason in PHP why it should do that. There is very little overhead
in PHP for calling exec() in either Windows or Unix.

Then it is the way swetest.exe compiled in C is running on a Windows machine
versus the way a UNIX compiled version of swetest is running on a UNIX server.

>> Maybe look at why Windows is taking so long? For instance - does it
have to load a bunch of DLL's, are you short of memory or other
environmental considerations?

Any Windows program, either C or VB, loads run-time files of various sorts.
Obviously the VB run-time files are "worse" than the C files because my
little VB program took twice as long to run 140 loops.

What I probably need to do is call the functions directory out of a libswe.a
library (UNIX) or a swedll32.dll DLL (Windows). But I don't yet know how to
do that using PHP. Do you have any ideas?

Thank you.


Otis

Jerry Stuckle

unread,
Feb 5, 2008, 6:17:08 PM2/5/08
to
Otis wrote:
> >> is this a compiled program (.exe) on Windows, also? What language?
>
> Yes, compiled in C, I believe. Just for fun, I wrote a little program in
> VB5 that essentially does what this other compiled C Windows program
> does in just one of the loops that gets executed about 140 times (at
> about 38 msec a loop on Windows). To my astonishment, my little VB5
> compiled program, which is only 22kbytes) took twice as long as the C
> .exe program to process the 140 loops, 11.5 secs versus 5.5 seocnds. I
> know for a fact that VB5 and VB6 compiled programs are very fast. My
> desktop program of the same application runs lots more calculations,
> through many more loops, in about 3 seconds total. So it is the
> interaction between PHP and the .exe on Windows that is slow. The same
> PHP script with a UNIX ".exe" runs in 4 to 5 seconds.
>

It doesn't surprise me that the VBScript takes less time; C is a very
efficient language. Other high-level compiled languages. while fast,
can seldom meet the speed and memory footprint of a good C program.

> Here is the line of code that runs so differently, time-wise:
>
> exec ("swetest -bj$ejd -p$p_idx -fls", $out);
>
> My PHPEd profiler shows this line of code to be the time hog.
>

I would expect that. Your profiler is going to consider that LOC being
executed from the time you call it until the executable returns.

>
> >> No reason in PHP why it should do that. There is very little
> overhead in PHP for calling exec() in either Windows or Unix.
>
> Then it is the way swetest.exe compiled in C is running on a Windows
> machine versus the way a UNIX compiled version of swetest is running on
> a UNIX server.
>

You'll have a lot of interaction with the OS at this point. PHP has to
call the OS to load and execute the program. Of course, part of this
process is checking security, loading shared libraries, etc. There's a
lot of work which needs to be done to get a program started. Then
there's more work to do to finish it.

However, this is quite excessive. I've executed Windows programs from
PHP before without this big delay.

Of course, the other thing is how long it takes to to execute the
program with those statements from a command prompt. This would more
closely emulate what you're doing with exec().

> >> Maybe look at why Windows is taking so long? For instance - does it
> have to load a bunch of DLL's, are you short of memory or other
> environmental considerations?
>
> Any Windows program, either C or VB, loads run-time files of various sorts.
> Obviously the VB run-time files are "worse" than the C files because my
> little VB program took twice as long to run 140 loops.
>
> What I probably need to do is call the functions directory out of a
> libswe.a library (UNIX) or a swedll32.dll DLL (Windows). But I don't yet
> know how to do that using PHP. Do you have any ideas?
>
> Thank you.
>
>
> Otis
>
>

It's not easy - you need to create a PHP extension for it. Not hard
once you've done it a few times, but the first couple of times will have
you pulling your hair out. See php.net for details if you want to go
that route.

NC

unread,
Feb 5, 2008, 8:07:24 PM2/5/08
to
On Feb 5, 2:08 am, Otis <otie_nos...@cox.net> wrote:
>
> I have a PHP script that uses exec() to call a program to do
> certain calculations.
>
> The UNIX version of this program I call runs in about 4 to 5
> seconds. When I run the same PHP script using exec() to call
> the Windows equivalent of this same program on my Windows
> desktop, it takes about 32 seconds.

Here are some more or less random guesses...

Could this possibly have something to do with priority level
on Windows? Your executable could be running in low-priority
mode...

How memory-intensive is your executable? It's possible that
on a Unix machine it has enough physical memory, while on
Windows it has to use the swap file...

In a similar vein, how I/O-intensive is your executable?
If your Unix machine has a much faster hard drive compared
to the Windows machine, this could really speed up the
execution. Not to mention the fact that Windows has subpar
I/O capabilities, as evidenced by Linux/Samba outperforming
Windows as Windows network server on equivalent hardware...

Is it possible that the cross-platform program relies on
some low-level I/O technique that's fast on Unix and slow
on Windows?

> Is there something I need to set in php.ini to get PHP and
> Windows to work faster together?

No, your problem is most likely OS-related...

> By the way, I run a comparable compiled VB desktop program
> with no PHP, with many more iterations than the PHP script
> uses, and it does the same calculations and a bunch more in
> about 3 seconds.

So what? It may be running with higher priority, that's all...
What happens if you call the Windows version of the cross-
platform program directly from the command line?

> So the problem is not with the .exe file - it is a problem
> involving both PHP and Windows.

I sincerely doubt it...

> What is it about the interworking of PHP on Windows with
> Windows that is really slow?

Windows. :)

Cheers,
NC

Otis

unread,
Feb 6, 2008, 2:54:40 AM2/6/08
to
>> It's not easy - you need to create a PHP extension for it. Not hard
once you've done it a few times, but the first couple of times will have you
pulling your hair out. See php.net for details if you want to go that route.

I'm not sure I'm up to that just yet.

But here's what I did do.

After researching, I have made a small .dll file using VB6. It is called
astro_php.dll. Currently, it simply calculates the longitude and speed of
one given planet on one given day. The astro_php.dll is about 24k in size.

Then in my PHP script where I was previously calling exec(swetest . . . ), I
have replaced this with:

Function Get_1_Planet_geo($jd, $p_idx)
{
unset($output,$long_speed);
$obj = new COM("astro_php.astro_php_cls");
$output=$obj->Get_1_Planet(strval($jd), strval($p_idx));
$long_speed = explode(',',$output);
return $long_speed;
}

The time to do 140 loops, as this is what the script does for this
particular function, went from 5.5 seconds using swetest.exe and 11.5
seconds using a VB5 program I wrote to try to replace swetest.exe to:

60 millisec

BINGO!


The only thing I do not yet understand is why I must pass strings to the
astro_php.dll function instead of doubles or integers. But with the speed
gained above, who cares, right?

Now I have to work on replacing the loop that runs 562 times and takes a
total of 22.6 seconds with the above and see how much time I save (I did
this and it went from 22.6 seconds to less than a second, I think).

So, by converting a VB6 DLL into a COM and calling the COM from PHP I am
able to work around the speed limitations of swetest.exe, PHP, and Windows.


Thanks to all for your help.


Allen

Jerry Stuckle

unread,
Feb 6, 2008, 1:30:26 PM2/6/08
to

Good. That bypasses your problem, anyway. But what about next time?
In this case I'd be interested in determining just why the delay is
occurring.

For instance - as I said, try running it from a command line prompt with
the same parameters. How long does it take?

Also do something like write the start time to a file at the start of
the module and the end time just before it finishes. Do the same just
before and after the exec() call (they can even be the same file if you
close the file in PHP first). Such a time lag should be very noticeable.

0 new messages