3

I implemented Dijkstra's algorithm in Python and ran the script under Ubuntu and windows 8. Both x64 architecture.

I profiled the script using python -m cProfile name.py

I was surprised to see that program took half the time in Ubuntu compared to Windows.

Ubuntu run time for 1000 x 1000 matrix : 0.4 sec

Windows run time for 1000 x 1000 matrix = 1.2 sec

This is on the same, dual booted, machine.

How this is happening ?

James Z
  • 12,209
  • 10
  • 24
  • 44
pa1pal
  • 743
  • 3
  • 14
  • 32
  • same version of python in both instances? Was this the first time the python had been run on Windows but it had been run previously on ubuntu? – Matt Coubrough Dec 15 '14 at 07:10
  • 1
    But you have shown `1.21 sec` for windows which is less than `0.4 sec` ? – Tanveer Alam Dec 15 '14 at 07:11
  • @MattCoubrough same python version 2.7 . I usually use python on this system. – pa1pal Dec 15 '14 at 07:12
  • Can you give the command used to time the run? Maybe it includes sytem time as well alongwith run time for script. – Tarun Gaba Dec 15 '14 at 07:13
  • python -m cProfile name.py – pa1pal Dec 15 '14 at 07:13
  • @BhargavRao which architecture. I boot my system open nothing just terminal and run the program. in both case , but why ubuntu took less time as compare to windows – pa1pal Dec 15 '14 at 07:17
  • @pa1pal Are both x64? – Bhargav Rao Dec 15 '14 at 07:18
  • @pa1pal The same happened in my case, Ubuntu was almost 10 times faster than windows, when I realized that there were many contributing factors. 1. The number of daemonic processes in windows is more than Ubu (atleast on my system) 2. The swap areas are quite different. These were the two reasons in my case. There may be many other reasons. Do take care of these two issues and check again. (Even I am waiting for the answer) – Bhargav Rao Dec 15 '14 at 07:23
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/66889/discussion-between-pa1pal-and-bhargav-rao). – pa1pal Dec 15 '14 at 07:25

2 Answers2

0

First - the obvious is that while the versions of python might be the same, they have been compiled with different compilers and naturally that means the optimization levels are completely different. You could recompile python for both systems using gcc for example and run the whole thing again. However I will advise you to do this only if you are sure about what you are doing. More specifically for linux since many of the processes which run depend heavily on python. Take a look at this discussion.

Second - windows naturally takes up a lot more resources than linux and out of the box windows starts significantly more processes and every process is generally an infinite loop, taking up resources.

Comparing performance based on OS is a wrong concept. It's basically like comparing apples to oranges. Each system has different ways of managing it's memory and the running processes. The file system is another very important part - since python is in most cases an interpreted language, each import is performing disk operations.

Community
  • 1
  • 1
Alexander Ejbekov
  • 5,594
  • 1
  • 26
  • 26
  • "Composing processes based on OS is a wrong concept". What? There are a million reasons why this could have happened, but if those reasons are _solely_ environmental then I'll wrong my process on the quickest. That is a good reason for comparing and doesn't seem "wrong". – Ben Dec 15 '14 at 07:34
  • PS. my company is currently comparing 2 different versions of SLES with our current Solaris install. ZFS behaves differently and we can get between 200 and 700 MB/S of reads per second. Now is the OS important? – Ben Dec 15 '14 at 07:36
  • @Ben It is because there is no such thing as "perfect vacuum" once the os has booted up. As I said out of the box Linux is significantly more efficient at managing it's memory than Windows. But that is out of the box. The only way to have a somewhat good idea of how a system performs is to cut off all the fat and then run tests.  In Linux this is pretty much doable. In Windows - not really. – Alexander Ejbekov Dec 15 '14 at 08:05
  • First you mention Python as being compiled. Then in the same answer you say Python is interpreted in most cases. Seems like a contradiction... – WiseDev Aug 05 '19 at 08:19
0

Actually, there are a few things that were reason for slow performance of Python in my machine. However, I don't have statistics with me and so no proof from my side. You might have to check them as well(with a pinch of salt as well ;-)).

  • Antivirus - If any antivirus is there, add Python location as well as the program's location to the whitelist and then try running them; this actually gained me nearly 33% performance
  • Process Priority - Give high priority to the process while running(in my case it was a long running process so I just do it through the task manager)
  • Proper drivers - Believe me. Even though this sounds something stupid, upgrading the device drivers does improve the performance. Case in point is when I run the python process from an external hard disk, it ran a little better when compared to the internal hard disk. Once I updated my hard disk driver, it actually started running better than my external hard disk(It is to be noted that it was still slower than the Linux version)

However, these might not still help you to reach the performance like Linux. Main reason is that both python might be compiled by different c compilers. Also libc might be running.

thiruvenkadam
  • 4,170
  • 4
  • 27
  • 26