{"id":147,"date":"2008-08-06T00:10:43","date_gmt":"2008-08-06T06:10:43","guid":{"rendered":"http:\/\/www.philhassey.com\/blog\/?p=147"},"modified":"2008-08-06T00:10:43","modified_gmt":"2008-08-06T06:10:43","slug":"comparing-python-to-c","status":"publish","type":"post","link":"https:\/\/www.philhassey.com\/blog\/2008\/08\/06\/comparing-python-to-c\/","title":{"rendered":"Comparing python to C &#8230;"},"content":{"rendered":"<p>Well, I&#8217;m about 1\/2 done with adding multi-players to Galcon-iphone.\u00a0 I figured I&#8217;d stop for five minutes and note down some things I noticed during my porting experience.\u00a0 Obviously, C and python are different, but here are a few of the ways I&#8217;ve FELT the difference.<\/p>\n<ul>\n<li>C doesn&#8217;t have garbage collection.\u00a0 I&#8217;ve managed to avoid doing much manual GC by having everything in a single big struct that keeps the entire state.\u00a0 The only place I use malloc\/free is when I&#8217;m downloading web data.\u00a0 So it hasn&#8217;t been a big deal for this project.<\/li>\n<li>Speaking of web data, C doesn&#8217;t have a built in urllib.\u00a0 I wrote my own awful HTTP 1.0 page fetcher.\u00a0 On the plus side, I was able to make it work *exactly* how I wanted it.\u00a0 No threads, non-blocking \ud83d\ude42<\/li>\n<li>Although not &#8220;built in&#8221; I found enet, which was a thin layer above UDP for networking.\u00a0 I found this incredibly much easier to use than python sockets were.\u00a0 (Yes, I could have used some other python lib, but I didn&#8217;t.)<\/li>\n<li>C is really fast.\u00a0 Thankfully, since I originally dev&#8217;d Galcon in python, I was &#8220;forced&#8221; to get my algorithms pretty good to keep the speed reasonable.\u00a0 By porting everything to C, I&#8217;ve now got amazing speed.\u00a0 I think I could host 100+ servers on a single CPU easily.<\/li>\n<li>Managing strings in C is an ordeal.\u00a0 It&#8217;s just not pretty, so I try to avoid doing much with them.<\/li>\n<li>OpenGL seems pretty painless in C.\u00a0 I haven&#8217;t done any notable OpenGL stuff in python, so I can&#8217;t really compare the two.<\/li>\n<li>No exception handling makes me have to code things more carefully.\u00a0 In python, I could parse an incoming networking packet, and just wrap it in a &#8220;try: (parse stuff) except: pass&#8221; block.\u00a0 If something was bad about the packet, it would just move on.\u00a0 In C, I have to check everything carefully so that I don&#8217;t create some kind of segfault by accident.<\/li>\n<li>In my case, serializing data is easy in C.\u00a0 I can just save a structure to a file (or send it in a net packet) and load it back later.\u00a0 In python this is much more complicated.\u00a0 (Especially since Galcon-shareware isn&#8217;t pure-python.)<\/li>\n<li>By using C, I don&#8217;t have to deal with some awful FFI.\u00a0 That is a HUGE time-saver.<\/li>\n<li>I must admit, I LOVE python&#8217;s indents.\u00a0 But I also must admit, now that I&#8217;m back into C coding, I don&#8217;t miss them as much as I thought I would.<\/li>\n<li>I still dislike header files.\u00a0 I wish C magically generated them or something instead of making me write them.<\/li>\n<li>I learned about writing tests in python.\u00a0 I&#8217;m doing that in C for my networking code.\u00a0 I run the tests as part of my build process, and it saves loads of time by catching all the bugs for me \ud83d\ude42<\/li>\n<li>Automatic type checking is sort of nice.\u00a0 Having the compiler tell me I didn&#8217;t do anything incredibly stupid is sort of nice.\u00a0 (Although in some cases, it can get a bit annoying.)<\/li>\n<\/ul>\n<p>Well, that&#8217;s about all I can think of for now.\u00a0 Draw your own conclusions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Well, I&#8217;m about 1\/2 done with adding multi-players to Galcon-iphone.\u00a0 I figured I&#8217;d stop for five minutes and note down some things I noticed during my porting experience.\u00a0 Obviously, C and python are different, but here are a few of the ways I&#8217;ve FELT the difference. C doesn&#8217;t have garbage collection.\u00a0 I&#8217;ve managed to avoid [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,20,19,66,2],"tags":[],"class_list":["post-147","post","type-post","status-publish","format-standard","hentry","category-development","category-galcon","category-gamedev","category-iphone","category-python"],"_links":{"self":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts\/147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/comments?post=147"}],"version-history":[{"count":0,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts\/147\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/media?parent=147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/categories?post=147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/tags?post=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}