File: c_sort.py

package info (click to toggle)
python-pyinstrument 5.1.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,620 kB
  • sloc: python: 6,713; ansic: 897; makefile: 46; sh: 20; javascript: 18
file content (36 lines) | stat: -rw-r--r-- 909 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""
list.sort is interesting in that it calls a C function, that calls back to a
Python function. In an ideal world, we'd be able to record the time inside the
Python function _inside_ list.sort, but it's not possible currently, due to
the way that Python records frame objects.

Perhaps one day we could add some functionality to pyinstrument_cext to keep
a parallel stack containing both C and Python frames. But for now, this is
fine.
"""

import sys
import time

import numpy as np

arr = np.random.randint(0, 10, 10)

# def print_profiler(frame, event, arg):
#     if event.startswith('c_'):
#         print(event, arg, getattr(arg, '__qualname__', arg.__name__), arg.__module__)
#     else:
#         print(event, frame.f_code.co_name)

# sys.setprofile(print_profiler)


def slow_key(el):
    time.sleep(0.01)
    return 0


for i in range(10):
    list(arr).sort(key=slow_key)

# sys.setprofile(None)