Skip to content

Instantly share code, notes, and snippets.

@aphi
Created April 1, 2023 01:20
Show Gist options
  • Save aphi/4b2f7cfc7ccc52465bead3c24ee9c3f4 to your computer and use it in GitHub Desktop.
Save aphi/4b2f7cfc7ccc52465bead3c24ee9c3f4 to your computer and use it in GitHub Desktop.
Highspy Pybind11 Interface Speed Comparison
"""
Outputs:
Time taken to solve: 10.0589
__getitem__ method for 134322 vars: 162.8729
Copy-first method for 134322 vars: 0.0042
"""
import highspy
import timeit
h = highspy.Highs()
h.readModel("bin_packing.mps")
h.setOptionValue("time_limit", 10)
# Solve
start_time = timeit.default_timer()
h.run()
elapsed = timeit.default_timer() - start_time
print(f"Time taken to solve: {elapsed:.4f}")
solution = h.getSolution()
num_vars = h.getNumCol()
# Extract values using __getitem__ N times
start_time = timeit.default_timer()
values = [solution.col_value[icol] for icol in range(num_vars)]
elapsed = timeit.default_timer() - start_time
print(f"__getitem__ method for {num_vars} vars: {elapsed:.4f}")
# Extract values by copying first to list
start_time = timeit.default_timer()
col_values = list(solution.col_value)
values = [col_values[icol] for icol in range(num_vars)]
elapsed = timeit.default_timer() - start_time
print(f"Copy-first method for {num_vars} vars: {elapsed:.4f}")
@aphi
Copy link
Author

aphi commented Apr 5, 2023

image

@HaoZeke
Copy link

HaoZeke commented Sep 30, 2023

@aphi could you add also the bin_packing.mps file needed to reproduce this fully?

@aphi
Copy link
Author

aphi commented Sep 30, 2023

@HaoZeke Sure, here it is. I zipped it since the full mps file is 30 MB which exceeds GitHub limits.

https://github.com/aphi/0/raw/main/bin_packing.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment