3

This is the laptop sitting in front of me right now:

enter image description here

On it is installed VMWare Fusion 8.5.3 with an Ubuntu 16.04 64 bit VM. I ran the following little test to compare performance between bash on the host and bash on the VM:

time for i in {1..1000000}; do :; done

Note that the stock bash version on MacOS is 3.2.57, so I downloaded and built the same version to run on the VM to ensure a fair comparison.

The results - The VM is more than 2x as fast as the host!

Host (MacOS)

mymac:~ me$ time for i in {1..1000000}; do :; done

real    0m4.608s
user    0m4.536s
sys 0m0.067s
mymac:~ me$

VM (Ubuntu)

ubuntu@ubuntu:~$ time for i in {1..1000000}; do :; done            

real    0m2.146s
user    0m2.136s
sys 0m0.008s
ubuntu@ubuntu:~$

How can the VM be so much faster when it should be effectively running the same code on (a virtualized form of) the same CPU?


Notes

  • The MacOS host has 8 logical cores and 16GB RAM. I have assigned only 2 logical cores and 2GB RAM to the VM. I think this should make little difference - this test is not memory bound and is single-threaded.
  • I powered off the VM while running the host test
  • I ran strace on the Ubuntu bash process. As expected there just a handful of syscalls at the start and end of the test, and no syscalls during the bulk of the test (the for loop). So this test should largely be CPU-bound and not affected by differences in I/O implementation between MacOS and Ubuntu.
  • I ran the test several times and the result seems fairly consistent.
  • There is no other notable system load.
  • Can you test again with a for loop instead (time for ((i=0; i < 1000000; i++)); do :; done)? That should be interesting... – xhienne Jan 20 '17 at 1:25
  • @xhienne The results are similar: >twice faster on the VM. In fact the for (( )) version took about twice as long as the for x in { } on both host and VM. – Digital Trauma Jan 20 '17 at 1:33
  • It's twice as long but if you consider the memory usage, you'll see a huge difference (change 1000000 to 100000000 if that's not obvious enough). But I expected your host to be quicker than the VM in my test, I'm disappointed – xhienne Jan 20 '17 at 1:36
  • I guess your two cores are dedicated to the VM and that no other process on your host can use these two cores. Can you dedicate a core to your bash process on your host, just to be sure your Mac is not using it to do other tasks? – xhienne Jan 20 '17 at 1:45
  • 1
    At this time, my only conclusion is that the compiler used on your MacOS to compile bash is less efficient than gcc on Ubuntu. Or the compiler flags are chosen less carefully. – xhienne Jan 20 '17 at 2:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.