
PHP and C#
Posted Saturday, 14 July, 2012 - 03:33 by flopolocoThis post is not related to OpenTK, but it won't hurt to share it because it involves C# code. I primarily code web applications in PHP due to work reasons, I find PHP to be very practical and easy to work but a great disadvantage of it is that is very slow.
There is a solution though to go to Zend stack, but that is another issue, whether or not to migrate to a Zend server or stick to the classic open source PHP, I won't explore it now.
So a quick and dirty way to get your work done, why not use C# instead. I like C# better as a language than any other like Python, Pearl, C++ so why not use it as a CGI one? One matter though is whether or not your server supports MonoFramework, it seems that FreeBSD has support for Mono (not tested it yet) http://www.mono-project.com/Mono:FreeBSD so it seems that Mono is well respected and currently mature. Last but not least, use a Windows PHP web hosting plan, prices are almost the same.
C# Code: Compile this file and make an executable. Place the executable along with the index.php (or wherever you want as long as the program is accessible)
using System; namespace PhpCsharp { class Program { // http://www.dotnetperls.com/fibonacci static int Fibonacci(int n) { int a = 0; int b = 1; for (int i = 0; i < n; i++) { int temp = a; a = b; b = temp + b; } return a; } [STAThread] public static void Main(string[] args) { for (int i = 0; i < 10000; i++) { Fibonacci(i); //Console.Write(Fibonacci(i).ToString() + "<br>"); } } } }
PHP Code: Use this at the "index.php" as the entry-gateway
<?php function phpFibonacci($n) { $a = 0; $b = 1; for ($i = 0; $i < $n; $i++) { $temp = $a; $a = $b; $b = $temp + $b; } return $a; } $timeStart = microtime(true); print(shell_exec("PhpCSharp.exe")); $timeTaken = microtime(true)-$timeStart; print("C# taken: " . $timeTaken . " microseconds<br><br>"); $timeStart = microtime(true); for ($i = 0; $i < 10000; $i++) { phpFibonacci($i); } $timeTaken = microtime(true)-$timeStart; print("PHP taken: " . $timeTaken . " microseconds<br><br>"); ?>
Normally in C# you run your program and write the results to console. Then php can actually print any data it receives. For the benchmarking purposes though I disabled any sort of printing (neither C# outputs, neither php) just to get clearer results and not count in any character-stream-transfer timing.
If you are anxious to see the results... I won't keep you waiting.
C# taken: 0.21728301048279 microseconds PHP taken: 11.95049905777 microseconds
There you have it, very simple and effective.
P.S. I guess that this blog post in relation to IronPHP is worthless. :D
- flopoloco's blog
- Login or register to post comments

