How does one use `dis.dis` to analyze performance?

2024/10/4 15:37:13

I'm trying to use python's dis library to experiment with & understand performance. Below is an experiment i tried, with the results.

import disdef myfunc1(dictionary):t = tuple(dictionary.items())return tdef myfunc2(dictionary, func=tuple):t = func(dictionary.items())return t

>>> dis.dis(myfunc1)

  4           0 LOAD_GLOBAL              0 (tuple)3 LOAD_FAST                0 (dictionary)6 LOAD_ATTR                1 (items)9 CALL_FUNCTION            012 CALL_FUNCTION            115 STORE_FAST               1 (t)5          18 LOAD_FAST                1 (t)21 RETURN_VALUE 

>>> dis.dis(myfunc2)

  4           0 LOAD_FAST                1 (func)3 LOAD_FAST                0 (dictionary)6 LOAD_ATTR                0 (items)9 CALL_FUNCTION            012 CALL_FUNCTION            115 STORE_FAST               2 (t)5          18 LOAD_FAST                2 (t)21 RETURN_VALUE    

Now, i understand that...

  • the 4 & 5 on the far left are the line numbers
  • the column in the middle is the opcodes that are called by the machine
  • the column on the right are the objects (with opargs?)

...But what does this all mean in terms of performance? If i were trying to make a decision on which function to use, how would i use dis to compare the two?

Thanks in advance.

Answer

You (or at least regular people) can't look at different assembly codes, and tell which one is faster.

Try %%timeit magic function from IPython.

It will automatically run the piece of code several times, and give you an objective answer.

I recently found this blog post that teaches how to measure these kind of things in Python. Not only time, but memory usage too. The higlight of the post (for me, at least) it's when it teaches you to implement the %lprun magic function.

Using it, you will be able to see your function line by line, and know exactly how much each one contribute to the total time spent.

I've been using for a few weeks now, and it's great.

https://en.xdnf.cn/q/70591.html

Related Q&A

How do I require HTTPS for this Django view?

(r^login/?$,django.contrib.auth.views.login,{template_name:login.html, authentication_form:CustomAuthenticationForm}),How do I add HTTPS required to this? I usually have a decorator for it..But in th…

How many times a number appears in a numpy array

I need to find a way to count how many times each number from 0 to 9 appears in a random matrix created using np.random.randint()import numpy as np p = int(input("Length of matrix: ")) m = np…

python: How to remove values from 2 lists based on whats in 1 list

I have 2 lists of numbers, one called xVar and the other called yVar. I will use these 2 elements to plot X and Y values on a graph. They both have the same number of elements. Normally, I would jus…

merge two dataframe columns into 1 in pandas

I have 2 columns in my data frame and I need to merge it into 1 single columnIndex A Index B 0 A 0 NAN 1 NAN 1 D 2 B 2 …

Upsample and Interpolate a NumPy Array

I have an array, something like:array = np.arange(0,4,1).reshape(2,2)> [[0 12 3]]I want to both upsample this array as well as interpolate the resulting values. I know that a good way to upsample an…

How to extract text from table in image?

I have data which in a structured table image. The data is like below:I tried to extract the text from this image using this code:import pytesseract from PIL import Imagevalue=Image.open("data/pic…

numpy.savetxt tuple index out of range?

Im trying to write a few lines into a text file, and heres the code I used:import numpy as np# Generate some test data data = np.arange(0.0,1000.0,50.0)with file(test.txt, w) as outfile: outfile.w…

Retrieve list of USB items using Python

How can I retrieve the items plugged into the computer through USB using python? Ive searched around on here and found some old examples which dont appear to work anymore as they are over 5 years old.…

History across ipdb sessions

This question has been asked before, but I couldnt find a good answer. So, I am trying to ask again.I would like my ipdb to remember commands across sessions. Right now, it can pull up commands execute…

Python Distributed Computing (works)

Im using an old thread to post new code which attempts to solve the same problem. What constitutes a secure pickle? this?sock.pyfrom socket import socket from socket import AF_INET from socket import…