ProgrammingHardGATE CS · Product-company placement
In CPython, two threads each run a pure-Python CPU-bound loop (no I/O, no C extensions releasing the lock) on a 4-core machine. Compared with running the two loops one after the other in a single thread, the total wall-clock time with threads is typically:
- A. About half, because each thread gets its own core
- B. About a quarter, because CPython uses all 4 cores
- C. About the same or slightly worse, because the GIL lets only one thread execute Python bytecode at a time
- D. Unpredictable, because the threads corrupt each other's local variables
Correct answer: C. About the same or slightly worse, because the GIL lets only one thread execute Python bytecode at a time
The Global Interpreter Lock (in the standard, non-free-threaded CPython build) serialises bytecode execution, so CPU-bound threads do not run in parallel; switching overhead can even make it slower. Use multiprocessing for CPU-bound parallelism.
More Python Programming questions
- Which statement about iterables and iterators is correct?
- Assertion (A): After t = (1, 2, [3]), the statement t[2].append(4) succeeds and changes what print(t) shows. Reason (R): A tuple's…
- Which of these expressions raises an exception in Python 3?
- Which statement about the C3 method resolution order is correct for class D(B, C) where both B and C inherit from A?
- Which is the safest way to remove every even number from a list nums in place (so that other references to the same list see the change)?
- What is printed? python import sys a = [] b = a c = [a, a] print(sys.getrefcount(a) - sys.getrefcount([]) >= 3, c[0] is c[1], [a] == [b])