3

I had converted a MATLAB .m file into a exe file and had call it from php. But the execution time is veryy slow. So, now I am thinking of converting the matlab file into C.

Can I call the converted C file from PHP extension?

Do I need to first convert the matlab program into C and then call it from the php extension or I should convert the matlab program in C in PHP itself?

Will the execution time will be faster than before or will be the same?

vascowhite
  • 18,120
  • 9
  • 61
  • 77
user1583647
  • 1,227
  • 2
  • 24
  • 48

2 Answers2

1

You can possibly acquire some bechmarks to help choose which path to take.Can you execute the matlab and exe file manually? if so, how long do they take to execute?

I think that you have three options:

1) whatever the matlab file is doing, rewrite it in PHP. Execute the php and compare with the benchmarks above.

2) research the mechanism that you are using to execute the matlab exe file and see if it can be streamlined... are you using exec()?

3) convert & compile the matlab file into C and use the exec() mechanism, if available for your platform (Windows?) and compare to the benchmarks above. Can you describe how it is currently being executed?

Hope this helps!

Mitayai
  • 26
  • 3
  • Please follow this link below I had posted this question but was unable to get the answer.Anyhelp will be highly appreciated.http://stackoverflow.com/questions/15967157/matlab-executable-too-slow – user1583647 Apr 16 '13 at 04:19
  • writing an extension to php is something beyond all but the hard-core php people. look to see if someone has already done it before thinking of that route! have you considered recreating whatever matlab is doing using actual php code instead? – Mitayai Apr 16 '13 at 04:35
  • the matlab program is really big its basically for feature extraction of images.What if I convert the program into C apart from writing php extension how can I call it?using exe?will it be same as calling the console based application of matlab or different any idea? – user1583647 Apr 16 '13 at 04:42
  • if you use [exec()](http://www.php.net/manual/en/function.exec.php) it would basically be the same as calling the binary, but with a little overhead of the php itself having to fork a shell to execute the command. – Mitayai Apr 16 '13 at 04:49
0

If the converted C file has functions or class as interface, it can be wrapped into php extension and called from php.

Suppose you have a C file which has the following code:

int functionA(int p1,int p2);
int functionB(char p3,double p4);

You should compile this into static or dynamic library, e.g. example.so or example.a Then you should start writing php extension, see php extension hello world. In your extension, you will call the functions provided by example.so or example.a, and then provide interfaces to php namespace.

After doing all this, you can call functionA and functionB indirectly in php. I assure you that, code executed in C will be much faster than that in matlab or php itself.

cedricliang
  • 352
  • 1
  • 7