3

I know how to program in Python but I am also interested in learning C++. I have heard that it is much faster than python and for the programs I am writing currently, I would prefer them to run as quickly and efficiently as possible. I know that a lot of that comes from just writing good code but I was also wondering if using another language, such as C++, would help.

While I was pondering this, I realized that since most of my programs will be mainly using the internet (as in implementing Google APIs and using the information from them to submit data to other websites) then maybe the speed of the language doesn't matter if the speed of my internet connection is always going to be relatively the same. I have two ways I am connecting to the internet: Selenium (or some kind of automated browser) for things that require a browser, and just HTTP requests.

How much difference would I see between python and a different language even though the major focus of my programs is on the internet?

Thanks.

jacob501
  • 345
  • 2
  • 5
  • 17
  • 13
    In one word: zero. Python is by all means fast enough for HTTP requests (which, besides, are implemented natively, anyway). Entire content management systems that serve hundreds of users simultaneously are written in scripting languages. – Damon Nov 21 '12 at 01:47
  • 2
    Everyone wants their programs to run as quickly and efficiently as possible. But you also need to consider how easy the program is to write/maintain and the best ways to avoid bugs, etc. – John La Rooy Nov 21 '12 at 01:54
  • Yeah, this is a fairly simple program. Shouldn't take me too long to write. I was just debating which language to use (and if I should start my C++ learning with it) – jacob501 Nov 21 '12 at 01:56
  • 3
    If you really want to, you could write a first draft in python, then run it through a profiler. After reading the profiler's output, if you still feel that you need to optimize, then optimize your python code. If you want to optimize further, start looking into C++. But like @Damon said, python's requests are written natively i.e. in C. So I don't know how much more performance any C++ you write will be able to get you – inspectorG4dget Nov 21 '12 at 02:11
  • 1
    If you want to learn C++ by doing this little project then by all means do it in C++ to learn it. The issue in terms of performance is of your internet connection speed. Like a lot of programs the I/O times are the true performance bottle necks and not the actual language or virtual machine. The only advantage I can see is if you have a lot of complex processing that needs to happen on the returned data where C++ could give you an advantage. – sean Nov 21 '12 at 02:32
  • Okay. Thanks everyone! I might do what inspectorG4dget and sean suggested. – jacob501 Nov 21 '12 at 02:40
  • In respect of @sean, I do not even recommend C++ for learning purposes here (and this is a C++ programmer with a strong dislike for scripting languages talking!). C++ is a great language, but for such a task it is really the wrong tool. Also, it is better to learn a language like C++ on something that is not so inherently error-prone and possibly harmful. For example, python knows no such thing as a buffer overrun or a dangling pointer, and things that should always work 100% reliably (like a HTTP request) _just work_. Think about language performance when you write something like Facebook. – Damon Nov 21 '12 at 10:04
  • about I/O performance in Python and C++: [Why is reading lines from stdin much slower in C++ than Python?](http://stackoverflow.com/questions/9371238/why-is-reading-lines-from-stdin-much-slower-in-c-than-python) – jfs Nov 26 '12 at 11:41

1 Answers1

2

Scenarios

The main benefit you would get from using language that is compiled to machine code is that you can do lots of byte and bit-magic. Lets say, modifying image data, transforming audio, analysing indices of a genomic sequence database.

Typical tasks

Serving web-pages you typically have problems if a completely different sort: You will be loading a resource from hard disk, serve them directly if its an image or audio, or you will be executing different transformation steps on a text resource until it becomes the final HTML document. The latter will be using template engines, database queries, and so on.

If you look at that you can see that most of the things, say 90-99% are pretty high-level stuff -- in Python you will use an API that is optimized by many, many users for optimal performance (meaning: time and space). "Open a file" will be almost as fast in C as it is in Python, so is reading from it and serving it to some Socket. Transforming text data could be a bit faster in C++ then it is in Python, but... how fast does it have to be? A use is very likely willing to wait 200ms, isnt't he? And that is a lot of time for a nice high-level template engine to transform a bit of text.

What C++ and Python can do for you

A typical Python web-service is much faster to write and a easier to deploy then a server written in C++. If you would do it in C++ you firstly need to handle sockets and connections -- and for those would either use an existing library or write your own handling. If you use an existing library (which I strongly recommend) you are basically not doing anything differently then Python does. If you write your own handling, you have many, many low-level things you can do wrong that will burn the performance you wish for. No, that is not an option.

If you need speed, and Python and the server and template framework is not enough you should re-think your architectural approach. Then take a look at the c10k-problem and write tiny pieces in C. (Look at this c10k very hot topic, too) But I can not see many reasons not to use a high-level language like Python, if you are only looking for performance in a medium-complex web-serving application.

Summary: The difference

If you are just serving files from the hard-drive I guess your Python program would even be faster then your hand-crafted C++-server. If you use a framework written in C or C++ and just drop in your static pages, I guess you get a boost like 2-5fold against Python. Then again, if your web-application is a bit more complex then serving static content, I estimate that the difference will diminish very quickly and you will get 1-2fold speed gain at most.

It's not all about speed...

One note about another difference between C++ and Python one should not forget: Since C++ is really compiled and not as dynamic as Python you would gain a lot of static error analysis by using Python. Writing correct code is always difficult, but can be done in C++ and Python with good tests and static analysis -- the latter is simpler in C++ (my opinion). If that is an issue for you, you may think again, but you asked about speed.

Community
  • 1
  • 1
towi
  • 21,587
  • 28
  • 106
  • 187