3

I am toying with simple multi-threading program in both Python and C, where main starts multiple threads in a for loop and thread just do while(1). For both cases I ran 20 threads and observed CPU Utilization using 'top' with following outcomes:-

  1. C: 99.9% Userspace, 0% SysSpace
  2. Python: 11.3% Userspace, 10.7 SysSpace

Specs: 12 Core i7 8thGen. 16GB Ram

It would be great if someone can comment on why this is happening? Specially commenting on the way python and C execution works. Thank.

HIq
  • 105
  • 6
  • 3
    Do you build the C code with optimizations enabled? Any benchmarking without optimizations is really pointless. Also, are you sure that the code in both languages are doing the ***exact*** same thing? Even the slightest deviation could lead to big differences. – Some programmer dude Oct 16 '18 at 14:01
  • 2
    Without seeing the test programs this is hard to say. – Ctx Oct 16 '18 at 14:06
  • 2
    A `while 1: pass` in CPython is not executed *in parallel* - the global interpreter lock ensures that only *one* of the cores can be executing the Python bytecode ceval loop at any given time. Furthermore, Python bytecode interpreter will voluntarily yield to other threads after a certain number of instructions if there are other threads in *ready* state. This will mean a context switch through kernel, which is the reason for the big system space load. – Antti Haapala -- Слава Україні Oct 16 '18 at 15:15
  • Thank you @AnttiHaapala. Attached answers helped. – HIq Oct 16 '18 at 21:34

0 Answers0