Comparing python to C …
Wednesday, August 6th, 2008Well, I’m about 1/2 done with adding multi-players to Galcon-iphone. I figured I’d stop for five minutes and note down some things I noticed during my porting experience. Obviously, C and python are different, but here are a few of the ways I’ve FELT the difference.
- C doesn’t have garbage collection. I’ve managed to avoid doing much manual GC by having everything in a single big struct that keeps the entire state. The only place I use malloc/free is when I’m downloading web data. So it hasn’t been a big deal for this project.
- Speaking of web data, C doesn’t have a built in urllib. I wrote my own awful HTTP 1.0 page fetcher. On the plus side, I was able to make it work *exactly* how I wanted it. No threads, non-blocking 🙂
- Although not “built in” I found enet, which was a thin layer above UDP for networking. I found this incredibly much easier to use than python sockets were. (Yes, I could have used some other python lib, but I didn’t.)
- C is really fast. Thankfully, since I originally dev’d Galcon in python, I was “forced” to get my algorithms pretty good to keep the speed reasonable. By porting everything to C, I’ve now got amazing speed. I think I could host 100+ servers on a single CPU easily.
- Managing strings in C is an ordeal. It’s just not pretty, so I try to avoid doing much with them.
- OpenGL seems pretty painless in C. I haven’t done any notable OpenGL stuff in python, so I can’t really compare the two.
- No exception handling makes me have to code things more carefully. In python, I could parse an incoming networking packet, and just wrap it in a “try: (parse stuff) except: pass” block. If something was bad about the packet, it would just move on. In C, I have to check everything carefully so that I don’t create some kind of segfault by accident.
- In my case, serializing data is easy in C. I can just save a structure to a file (or send it in a net packet) and load it back later. In python this is much more complicated. (Especially since Galcon-shareware isn’t pure-python.)
- By using C, I don’t have to deal with some awful FFI. That is a HUGE time-saver.
- I must admit, I LOVE python’s indents. But I also must admit, now that I’m back into C coding, I don’t miss them as much as I thought I would.
- I still dislike header files. I wish C magically generated them or something instead of making me write them.
- I learned about writing tests in python. I’m doing that in C for my networking code. I run the tests as part of my build process, and it saves loads of time by catching all the bugs for me 🙂
- Automatic type checking is sort of nice. Having the compiler tell me I didn’t do anything incredibly stupid is sort of nice. (Although in some cases, it can get a bit annoying.)
Well, that’s about all I can think of for now. Draw your own conclusions.