Monday, May 30, 2011

Why your website should be written in C, not PHP

It's been awhile since I updated this blog, been pretty busy IRL.  Just recently got back from a Longsword Course with ARMA, in Ohio, was very nice, and I learned a lot.  I've also been playing the silver market recently; there's a pretty nice blog that I follow.  If you're looking for an interesting perspective on precious metals, check this guy at Along the Watchtower; he seems to know his stuff.

Okay, back to the point of this post. 

I have studied several scripting and programming languages; off the top of my head, I know Ada, C, C++, Fortran77, FORTH, Perl, PHP, Python, Java, and JavaScript.  Which language do I prefer, though?  C.

Now, for the record, just so you do know that there are plenty of sites written using something other than PHP.  Any website ending in .cgi are usually written in a compiled language.  I have came across some sites that are written in C++ before, and Deltapsi Studios, my good friend Stephan's site, is written in C, with mysql support and everything.

Anyway, more and more people in today's world are designing websites in PHP.  After all, as one of my professors would say, "PHP is built for the web!"  Maybe that's why Facebook uses PHP
Addendum 08/27/11: Actually it seems like Facebook wrote their site in PHP, then used software to convert it into a complied language, since interpreted PHP wouldn't be able to hold up the site.

C, on the other hand, I keep finding people consider it a low-level language that is only used by the hard-core programming.  One of those "hard to learn" languages that take millions of lines of code to get things done.

Despite the above accusations, you will find that C is actually a very good language.  It is an easy language to learn, and if programmed correctly, easy to understand, and by default, C is one of the fastest-executing programming languages out there, and when optimized, it can blow almost anything out of the water.  Check the end of my switch() vs elseif benchmarks post.  PHP took 8.169889 milliseconds to traverse my last test, whereas C took 0.01267 milliseconds.

Maybe you're thinking, "Okay, PHP takes 8ms and C takes 0.01ms.  A millisecond isn't that long."  There are two points I should make.  First, if you do the math between those two times, you find that C's switch() executed almost 645 times faster than PHP.  This in a matter of speaking, makes C about 64,500% faster than PHP!  Now THAT, is a speed difference.
And even then, while a millisecond may not seem that much to you, if you're having your web application serving hundreds or thousands or millions of requests, the speed difference will add up, and while the websites designed in C will be happily treading down the road, you'll find your PHP-site stalling and lagging while the preprocessor parses for your people.  For the virtual machine guys out there, if you want to get technical, ASP.NET or JSP should theoretically run faster than PHP.

Okay, so C is faster, C is more efficient, and I'm telling you that C is easier.  "But Anthony!" you cry, "What about the sessions and the cookies and the hash map arrays and the typeless variables, and my strings, and HTML output, and..."  Yes, yes.  By this point, you realize that PHP was indeed designed for the web, hence it will have a ton of web-design libraries that are not in the Standard C Library.  This means that if you want to use mysql, you will have to get the MySql Library and do your queries in C.  If you want sessions, you will have to find a session system for C, or write your own.

Yes, you will have to have "type" your variables, that is, using "int iNumber = 3;" instead of "$Number = 3;" and you will loose those hashmap/linked list arrays that everyone seems to love so much, though if you look at that previous benchmark, you'd find the hashmaps in PHP executing pretty slowly as well.  This is due to the slow nature of a hashmap, which is not in the scope of this entry, and just remember, in C, you can implement radix tables or something much, much, faster than hashmaps.

As for cookies, GET, and POST, it's really not that hard.  Below, you can read the get/post/cookie receiving that I used, in my C-web application, as well as the sessions system:

» Click to show Spoiler - click again to hide... «

There is a downfall, however in C, and that is how horrible it handles strings.  After all, with the happy-go-lucky $mystring = "Hi everyone!"; how can you go wrong?  In PHP, to concat a string, all you have to do is:
"Hi " . "Everyone!"
I believe.  Everyone knows that C-strings are slow.  Try to concat a string in C multiple times, and you'll quickly find that it has to traverse the string in strlen() every single time strcat() is called.  If String A and String B are 256 characters, and you strcat() them, when you try to concat the new string, it will be twice as slow.  Strings are a mess in C.  So what did I do?  I wrote a new string library, that handles strings like Pascal does:

struct string_t
  size_t len, /*Size of string to '\0' */
         max; /*Size of the total string*/
  char* str;

What's the point of the above structure?  Those that know C will probably be going "OH!", and for those who don't...  I'm storing the length of the string in my actual string.  You'll notice a HUGE increase in performance once strlen() can return the length with "str->len" instead of "for(s=str;*s;++s);"
So now, I can do,
string_t* mystr = mystr_create(256); //Make a string that can hold 256 characters.
mystr_concat(mystr, "Hello World!");
Was that so hard?

"What about outputting text?"  What about it?  It is true that in PHP, you can do:
<p>The number is: <?php echo $number ?></p>
Or whatever it is you do, but in C, I can just do:
printf("<p>The number is: %d<p/>", number);

What I'm doing, is simply embedding the HTML into the c-code, instead of embedding the c-code into the HTML.
(NOTE: Stephan, if you're reading this, no, I do not actually use printf..)

So in short, anything written in PHP can be written in C (even if you don't feel like writing the systems yourself, there's a good chance you can find a libcgi worth using), and PHP is slow (very slow).  Now, I can go over and discuss