Skip to content

Commit 5970ac2

Browse files
authored
Merge pull request #2225 from ERGO-Code/fix-2223
Added "Extracting values efficiently" to docs/src/interfaces/python/index.md
2 parents fc1f649 + b42c927 commit 5970ac2

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

docs/src/interfaces/python/index.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,32 @@ h.readModel(filename)
6767
h.run()
6868
print('Model ', filename, ' has status ', h.getModelStatus())
6969
```
70+
71+
## Extracting values efficiently
72+
73+
When arrays of values are returned by `highspy`, accessing them
74+
entry-by-entry can be very slow. Such arrays should first be converted
75+
into lists. The following example illustrates how the method
76+
`getSolution()` is used to obtain the solution of a model.
77+
78+
```python
79+
import highspy
80+
81+
h = highspy.Highs()
82+
h.readModel('model.mps')
83+
h.run()
84+
85+
solution = h.getSolution()
86+
num_vars = len(solution.col_value)
87+
88+
value = [solution.col_value[icol]
89+
for icol in range(num_vars)]
90+
91+
col_value = list(solution.col_value)
92+
value = [col_value[icol]
93+
for icol in range(num_vars)]
94+
```
95+
96+
For an example LP that is solved in 0.025s, accessing the values
97+
directly from `solution.col_value` takes 0.04s. Forming the list
98+
`col_value` and accessing the values directly from it takes 0.0001s.

0 commit comments

Comments
 (0)