Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker speeds up python, why?

So I am playing with docker for the first time on my mac. I used boot2docker through the standard tutorial and I am starting a prompt in an ubuntu image.

docker pull ubuntu
docker run -i -t ubuntu /bin/bash

When in docker I started my first experiment, to see if the performance would go down. From the command line I would use the python timeit module to quickly check some basic performance measures.

Mac Python Results
$ python3.4 -m timeit '"-".join(str(n) for n in range(100))'
10000 loops, best of 3: 37.7 usec per loop
$ python3.4 -m timeit '"-".join([str(n) for n in range(100)])'
10000 loops, best of 3: 34.2 usec per loop
$ python3.4 -m timeit '"-".join(map(str, range(100)))'
10000 loops, best of 3: 26.2 usec per loop
Docker Python Results
> python3 -m timeit '"-".join(str(n) for n in range(100))'
10000 loops, best of 3: 30 usec per loop
> python3 -m timeit '"-".join([str(n) for n in range(100)])'
10000 loops, best of 3: 26.9 usec per loop
> python3 -m timeit '"-".join(map(str, range(100)))'
10000 loops, best of 3: 20.2 usec per loop

It seems strange that the docker ubuntu, that is running on top of my mac, is actually running python code faster than the python on mac. Is there any reason for why this might be?

Edits

I can confirm that both python versions are running in 64 bit.

Mac Python
python3 -c 'import sys;print("%x" % sys.maxsize, sys.maxsize > 2**32)'
7fffffffffffffff True
Ubuntu Python
python3.4 -c 'import sys;print("%x" % sys.maxsize, sys.maxsize > 2**32)'
7fffffffffffffff True
like image 262
cantdutchthis Avatar asked Dec 06 '14 19:12

cantdutchthis


People also ask

Why Docker containers are fast?

Since the host kernel is shared amongst Docker containers, applications only ship with what they need to run—no more, no less. This makes Docker applications easier and more lightweight to deploy and faster to start up than virtual machines.

Is Docker good for Python?

Docker is a containerization tool used for spinning up isolated, reproducible application environments. It is a popular development tool for Python developers. The tutorials and articles here will teach you how to include Docker to your development workflow and use it to deploy applications locally and to the cloud.

Does Docker increase performance?

Docker containers are designed to run anywhere - in an in-house data center, the cloud, or a hybrid and multi-cloud environment. Out of the box, Docker containers tend to outperform other virtualization methods, but they can be further optimized to offer even better performance.

Which Docker image is best for Python?

If you want the absolute latest bugfix version of Python, or a wide variety of versions, the official Docker Python image is your best bet. If you want the absolute latest system packages, you'll want Ubuntu 22.04; RedHat 9 is somewhat more conservative, for example including Python 3.9 rather than 3.10.


1 Answers

This is more about the difference in operating systems than about docker performance. Measuring performance of applications can be tricky.

Bottom line is that OS X has a good number of processes that will compete with your test and OS X likely did not give your test a high priority.

A container should perform as well as the native environment (sometimes better) in most cases. But, your test should make the container do work. Docker will need to add overhead when your application is making system calls and accessing I/O, so those should both be included in your test.

IBM wrote a paper on the Linux Container vs. Native env issue last year.

http://domino.research.ibm.com/library/cyberdig.nsf/papers/0929052195DD819C85257D2300681E7B/$File/rc25482.pdf

like image 160
Christian Davis Avatar answered Oct 05 '22 16:10

Christian Davis