Python and C…Wow…

I’ve come to the realization that as nice as Python is, I really ought to learn a lower-level language. Even though I own two C++ books, I’m only reading them to a)Gain a rudimentary knowledge of the C++ syntax, such that I can hack it if I have to b)Improve my conceputal knowledge c)Well, they are interesting.

Instead, I’m learning C, because it just seems like more of my cup of tea. And kernel hacking might be fun :)

So one of the sample programs in the tutorial I’m using is that which will list all the numbers from 1-2500, and say if they’re prime or not. I faithfully wrote and compiled it in C, ran it, and WOW! that was fast. I could not detect any sort of delay, or processing or anything, it was just:

<Enter><A screenfull of numbers, ending in “2500 is not prime” appears>

I was so impressed by this, I converted the code to Python. I even removed a function that listed the version number and time compiled. When I ran it, I could see numbers whizzing by the screen for half a second or so, and then it was done.

I know it’s old news that compiled languages are faster than interpreted ones, and that low-level languages are faster than high-level ones, but it’s just impressive to tangibly see it.

Share/Save/Bookmark

4 Comments »

  1. Pete Said,

    February 7, 2009 @ 8:24 am

    I liked this post ^_^
    Could you upload the 2 codes?

  2. Timmy Said,

    February 7, 2009 @ 12:20 pm

    Here it is:
    http://blog.tsmacdonald.com/archives/220

  3. David Said,

    May 30, 2009 @ 1:55 pm

    Pyrex is supposedly almost that fast — it’s like C, coded as a close dialect of Python.

    You don’t get functional programming stuff or list comprehensions, and you have to initialize/declare variables, but if you hack at it a little, it’s supposed to run at about 40-75% of the speed of C. (Naturally, I can’t vouch for that, but it’s worth checking out.)

    I also heard tell that there’s a new version of Psyco in development that will be a Python-to-C translator. That’s become a popular way to design compilers. We’ll see if it shows up.

  4. Timmy Said,

    May 30, 2009 @ 4:56 pm

    Cython, too…

Leave a Comment