रनटाइम के लिए कोड प्रोफाइलिंग

Efficient Python Code लिखना

Logan Thomas

Scientific Software Technical Trainer, Enthought

कोड प्रोफाइलिंग

  • फंक्शन कॉल की आवृत्ति और अवधि के विस्तृत आँकड़े
  • लाइन-दर-लाइन विश्लेषण
  • उपयोग किया पैकेज: line_profiler
pip install line_profiler
Efficient Python Code लिखना

कोड प्रोफाइलिंग: रनटाइम

heroes = ['Batman', 'Superman', 'Wonder Woman']

hts = np.array([188.0, 191.0, 183.0])

wts = np.array([ 95.0, 101.0,  74.0])
Efficient Python Code लिखना
def convert_units(heroes, heights, weights):

    new_hts = [ht * 0.39370  for ht in heights]
    new_wts = [wt * 2.20462  for wt in weights]

    hero_data = {}

    for i,hero in enumerate(heroes):
        hero_data[hero] = (new_hts[i], new_wts[i])

    return hero_data
convert_units(heroes, hts, wts)
{'Batman': (74.0156, 209.4389),
 'Superman': (75.1967, 222.6666),
 'Wonder Woman': (72.0471, 163.1419)}
Efficient Python Code लिखना

कोड प्रोफाइलिंग: रनटाइम

%timeit convert_units(heroes, hts, wts)
3 µs ± 32 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
Efficient Python Code लिखना
%timeit new_hts = [ht * 0.39370  for ht in hts]
1.09 µs ± 11 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit new_wts = [wt * 2.20462  for wt in wts]
1.08 µs ± 6.42 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%%timeit
hero_data = {}
for i,hero in enumerate(heroes):
    hero_data[hero] = (new_hts[i], new_wts[i])
634 ns ± 9.29 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Efficient Python Code लिखना

कोड प्रोफाइलिंग: line_profiler

line_profiler पैकेज का उपयोग

%load_ext line_profiler

लाइन-दर-लाइन समय के लिए मैजिक कमांड

%lprun -f
Efficient Python Code लिखना

कोड प्रोफाइलिंग: line_profiler

line_profiler पैकेज का उपयोग

%load_ext line_profiler

लाइन-दर-लाइन समय के लिए मैजिक कमांड

%lprun -f convert_units
Efficient Python Code लिखना

कोड प्रोफाइलिंग: line_profiler

line_profiler पैकेज का उपयोग

%load_ext line_profiler

लाइन-दर-लाइन समय के लिए मैजिक कमांड

%lprun -f convert_units convert_units(heroes, hts, wts)
Efficient Python Code लिखना

%lprun आउटपुट

%lprun -f convert_units convert_units(heroes, hts, wts)

alt="मैजिक कमांड lprun का टेबलर आउटपुट जो रनटाइम प्रोफाइलिंग आँकड़े सारांशित करता है"

Efficient Python Code लिखना

%lprun आउटपुट

%lprun -f convert_units convert_units(heroes, hts, wts)

alt="मैजिक कमांड lprun का टेबलर आउटपुट जो रनटाइम प्रोफाइलिंग आँकड़े सारांशित करता है, जहाँ Line # और Hits कॉलम हाइलाइट हैं"

Efficient Python Code लिखना

%lprun आउटपुट

%lprun -f convert_units convert_units(heroes, hts, wts)

alt="मैजिक कमांड lprun का टेबलर आउटपुट जो रनटाइम प्रोफाइलिंग आँकड़े सारांशित करता है, जहाँ Time कॉलम हाइलाइट है"

Efficient Python Code लिखना

%lprun आउटपुट

%lprun -f convert_units convert_units(heroes, hts, wts)

alt="मैजिक कमांड lprun का टेबलर आउटपुट जो रनटाइम प्रोफाइलिंग आँकड़े सारांशित करता है, जहाँ पहली पंक्ति की Time unit हाइलाइट है"

Efficient Python Code लिखना

%lprun आउटपुट

%lprun -f convert_units convert_units(heroes, hts, wts)

alt="मैजिक कमांड lprun का टेबलर आउटपुट जो रनटाइम प्रोफाइलिंग आँकड़े सारांशित करता है, जहाँ तीसरी पंक्ति हाइलाइट है"

Efficient Python Code लिखना

%lprun आउटपुट

%lprun -f convert_units convert_units(heroes, hts, wts)

alt="मैजिक कमांड lprun का टेबलर आउटपुट जो रनटाइम प्रोफाइलिंग आँकड़े सारांशित करता है, जहाँ Per Hit कॉलम हाइलाइट है"

Efficient Python Code लिखना

%lprun आउटपुट

%lprun -f convert_units convert_units(heroes, hts, wts)

Magic command lprun tabular output summarizing runtime profiling statistics with line nine highlighted

Efficient Python Code लिखना

%lprun आउटपुट

%lprun -f convert_units convert_units(heroes, hts, wts)

alt="मैजिक कमांड lprun का टेबलर आउटपुट जो रनटाइम प्रोफाइलिंग आँकड़े सारांशित करता है, जहाँ % Time कॉलम हाइलाइट है"

Efficient Python Code लिखना

%lprun आउटपुट

%lprun -f convert_units convert_units(heroes, hts, wts)

alt="मैजिक कमांड lprun का टेबलर आउटपुट जो रनटाइम प्रोफाइलिंग आँकड़े सारांशित करता है, जहाँ Line Contents कॉलम हाइलाइट है"

Efficient Python Code लिखना
%timeit convert_units convert_units(heroes, hts, wts)
3 µs ± 32 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%lprun -f convert_units convert_units(heroes, hts, wts)

alt="मैजिक कमांड lprun का टेबलर आउटपुट जो रनटाइम प्रोफाइलिंग आँकड़े सारांशित करता है, जहाँ दूसरी पंक्ति का Total time हाइलाइट है"

Efficient Python Code लिखना

अपने नए प्रोफाइलिंग स्किल्स का अभ्यास करें!

Efficient Python Code लिखना

Preparing Video For Download...