Python timing comparison

On page 7 of Learning Python (4th ed), Mark Lutz briefly discusses the speed differences between Python and compiled languages like C++. I was curious as to how much slower an interpreted language is than a compiled language. To illustrate this, I whipped up a quick and simple program that exercises the CPU so I can see the difference — calculating 2^a-big-number, chosen at runtime, should do the trick. PHP is used in this example as another interpreted language to be compared against.

C++:

#include <stdio.h>
#include <cmath>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[]){
    printf("%lf\n", pow(2,atoi(argv[1])));
    return 0;
}

Python 3:

import sys
print(2 ** int(sys.argv[1]))

PHP 5.3:

<?php printf('%f', pow(2,$argv[1])); ?>

The Bash script to rule them all:

 echo 'C++ Compile and Run:'
 time { g++ 2powX.cpp -o 2powX; ./2powX $1; }
 echo ''
 echo 'C++ Run-only:'
 time { ./2powX $1; }
 echo ''
 echo 'Python3:'
 time { python3 2powX.py $1; }
 echo ''
 echo 'PHP 5.3:'
 time { php 2powX.php $1; }

To start the test, I just need to think of a large number (under 1024 to be fair to C++ and PHP):

./2powX.sh 1023

(this will calculate 2^1023)

My output:

C++ Compile and Run:
89884656743115795386465259539451236680898848947115328636715040578866337902750481566354238661203768010560056939935696678829394884407208311246423715319737062188883946712432742638151109800623047059726541476042502884419075341171231440736956555270413618581675255342293149119973622969239858152417678164812112068608.000000

real 0m0.393s
user 0m0.051s
sys 0m0.029s

C++ Run-only:
89884656743115795386465259539451236680898848947115328636715040578866337902750481566354238661203768010560056939935696678829394884407208311246423715319737062188883946712432742638151109800623047059726541476042502884419075341171231440736956555270413618581675255342293149119973622969239858152417678164812112068608.000000

real 0m0.023s
user 0m0.007s
sys 0m0.001s

Python3:
89884656743115795386465259539451236680898848947115328636715040578866337902750481566354238661203768010560056939935696678829394884407208311246423715319737062188883946712432742638151109800623047059726541476042502884419075341171231440736956555270413618581675255342293149119973622969239858152417678164812112068608

real 0m0.297s
user 0m0.000s
sys 0m0.048s

PHP 5.3:
89884656743115795386465259539451236680898848947115328636715040578866337902750481566354238661203768010560056939935696678829394884407208311246423715319737062188883946712432742638151109800623047059726541476042502884419075341171231440736956555270413618581675255342293149119973622969239858152417678164812112068608.000000

real 0m0.158s
user 0m0.032s
sys 0m0.001s

I am tossing out the “real” time since that can be affected by other processes. “User” and “sys” show time the CPU devoted to running those individual scripts. As such, the times for this one run can now be rewritten as:

C++ Compile and Run:
User + Sys: 0m0.080s

C++ Run-only:
User + Sys: 0m0.008s

Python:
User + Sys: 0m0.048s

PHP 5.3:
User + Sys: 0m0.033s

So, as can be seen the limited test above, Python is fairly whippy, but it is much slower than compiled C++ and only slightly slower than PHP. As should be expected, the clear loser is C++ when compiling and running the script.

Where Python shows its strength in this test is when I chose an exponent greater than 1023: 10230.

C++ Compile and Run:
inf

real 0m0.300s
user 0m0.031s
sys 0m0.042s

C++ Run-only:
inf

real 0m0.019s
user 0m0.005s
sys 0m0.000s

Python3:
34423548946370924386082176169924624476944747406217256894913459575909248317240966176805010055150062365680632964253221068333260790503850696894422205735464974283998737178481710847320513326190047236314022188737959298573273377572102814344914668123463409967698383046161842652734328087101543308616495804172173597443760150966561459556280381912226987600617860174561959409464229840988042610132190363959760816712340515107068363601259170162827462749834580914908129798594557386500003189308316950521690080348968238044726348143575742014074642846764730778606889774971100300380861398149094858913606975798199206609480173415963658004309039109726633755485225610220942041094784246272498178240736545885924645893162128769055657812870347841989821131743242903671864350280282225992429416035216789609308009640974003621570883792643521416611064873802839265130877016958833083099922397168644877343915843878494520657868082768244387134338616108424630943468980650164972598079418045598033655900759794230060922412992199496352809692058059391587557946774981437102060743632584473513113119693964019838269636314202681922917530324642652029487641966175702706370322376584585460708138993776570269329692750871949158552414912970025077446798138046851003712814450717667455166863392057966586288850960049888395606257059008819557399399606801132231135782721302679342447460405175159849415867946025083825979715054798665873771917391817852002180237432048544555977338839524157946665999976093796621072426879791910994074180568757224454625642236698005871493317583925305747386994569619246981512730972100283554651516588464599541520391279615376012683210012657907013238689991177931467599470681660217321117202829707966037830142127765807712314315920575387293970665233836678708372844700382542319872824604373900440287629673752417795738675580065061712084004815851934978942405039465510035530263041835571731680947077254161040170151053838823112861764994677500170596686600125442133816401312356669414594250360186893321405939078827468395808732729594844266313167495294959297094803384095616925536488530880160580375923278529891998055313008761747568315855046967664859083467206455946441917940607590639085228653276785225189773810221726842550754743851011255667524128744695091823443959124433576572372326104197569346540893424649163917453962544268485517137113038168451880755595073682103292050768360408537130764390007449122045773797009608394919316306798295728487217512556838653596654235979343724005701829082708397499442717395509370262664142587906309592105705900749680411391997494680768844358944926184889015762235955245288526033090092870443963596417673750181232848810929099290333172031916520155483111881817837853253295711864386467153079094803053093470817384183799736573767707463104819406906344594906925100612457971132594779011022054638441460108683414455611779738983530075370214072493728121365161763702953293237181723205188906292380268741274876582731547119810586813067313184416595624192660004425264601763282103986623523949088939973542755141592641945160825006847859413191687074741068848131241737183655870582681269756147276824562339232492246611538972567686798648566349824

real 0m0.291s
user 0m0.045s
sys 0m0.023s

PHP 5.3:
INF

real 0m0.173s
user 0m0.006s
sys 0m0.019s

C++ and PHP return “inf” or “INF”, which means the output was too great for the number type to handle. I could have fixed that by using additional or third-party libraries (ex. “gmp” in C++ and PHP), but that wouldn’t have been fair for this test. Without having to import any special libraries, Python was itching to output any number-size calculation I wanted to throw at it. If it calculated 2^10230 in 0.068s, how long would it take to calculate 2^10230000? 2m21.530s, but I won’t print the output here since it takes 3,079,537 characters to show.

So what did I learn from this limited test? Python is slower to run than compiled C++ and just a little slower than PHP 5; however, it is also much easier to get the information I need with Python out of the gate without having to include or import additional libraries, third-party or not. Python is definitely a language I will continue to explore.

Sharing main folders in Virtualbox

My dev environment is linux in a Virtualbox guest.

The following works in Windows, linux, and OSX hosts:

One thing I have begun to do in my Virtualbox guest is create a virtualbox shared folder with my host’s “my documents” or “documents” folder and name it “Documents.”

As a linux guest, I add my user to the “vboxfs” group. I then rename or delete my guest’s “documents” folder. I then recreate the guest’s “documents” folder so it is the same as my host’s “documents” folder: “ln -s /media/sf-Documents ~/Documents”

At this point, anything I add, delete, or change in my guest’s “Documents” folder is reflected immediately in my host’s “Documents” folder and vice versa.

For more information, check out the official Virtualbox documentation:
http://www.virtualbox.org/manual/ch04.html#sharedfolders

Cygwin

Cygwin allows you to have a unix-like environment within Windows. It gives you access to some unix tools and gives you a very limited unix environment. I like it because it allows me to treat the Windows command line environment more like the linux terminal. For instance, if I want to access a linux server via ssh, I can just fire up my Windows command line and access the server as I would had I been on a Mac or a linux box.

If there is a unix command which you cannot live without, launch the setup again and select that command to install it.

More information about Cygwin can be found on Wikipedia.

To install Cygwin in Windows

  1. Download Setup.exe from Cygwin
  2. Run setup.exe (keep this file so you can run it again to install or update packages)
    1. Next » “Install from Internet”
      • For the “root directory” I use “c:\cygwin”
      • Install for: all users
      • Default Text Type: Unix / Binary
    2. Next » “Local Package Directory”
      • I use “c:\cygwin-downloads”
    3. Next » “Use direct connection”
      • This should be good for most environments. Obviously, base it on your environment.
    4. Next
    5. Select one of the mirrors to download from
    6. Next (wait while packages download and install)
    7. Finish
  3. Add Cygwin’s bin directory to your Path statement.
    1. Windows XP:
      1. On the desktop, right-click “My Computer”, and click “Properties”
      2. In the System Properties window, click the “Advanced” tab
      3. Click the “Environment Variables” button
      4. In the “Systems Variables” section, find the “path” variable
      5. Click “Edit”
      6. Go to the start of “variable value” and type “c:\cygwin\bin;”
      7. Click “OK” until you are back on the desktop.
    2. Windows 7: Coming soon!
  • To be in the Cygwin environment, run “Cygwin Bash Shell” (Start » All Programs » Cygwin » Cygwin Bash Shell)
    • The prompt looks like: $
  • To be in the Windows environment, just run the Windows Command line (Start » Run » cmd)
    • The prompt lools like: c:\

If you used a DOS or Windows program to create your source code, you might need to convert the text to something with which unix is a little more comfortable before compiling with gcc. This is super-easy from the command prompt: dos2unix sourceCode.cpp. Going the other direction would be: unix2dos sourceCode.cpp.

On a dreary day, when the mood strikes you, you can curl up on the couch in front of the fireplace with your dog, a cozy blanket, and a nice bottle of shiraz. You can choose whether to spend that time with your sweetheart or the Cygwin Manual. My advice: the cygwin manual won’t drink all your wine.