sametz / nmrsim

A Python library for NMR simulation
MIT License
21 stars 6 forks source link

Increased the speed of _transition_matrix_dense twofold #26

Closed RaphaelRobidas closed 11 months ago

RaphaelRobidas commented 11 months ago

A small tweak the _transition_matrix_dense seems to increase its speed twofold.

Original code (nspins=8): _transition_matrix_dense took 33.4841s for 10000 runs

New code: _transition_matrix_dense took 16.7938s for 10000 runs

The equivalence has been verified for good measure as such:

for i in range(100):
    for j in range(100):
        m = i ^ j
        flag1 = bin(i ^ j).count("1") == 1

        flag2 = False
        if m > 0 and m & (m-1) == 0:
            flag2 = True

        assert flag1 == flag2
sametz commented 11 months ago

This looks like a neat trick! Can you make your pull request to the develop branch, and not master? See: DEVELOPERS.rst.

edit: also, let me know if you see GitHub Actions running code checks when you make a pull request to develop. If not, I may need to tweak them.

RaphaelRobidas commented 11 months ago

Sorry, it looks like I missed that info!

It looks like no Github Action was triggered. There isn't a "on pull request" trigger in the actions currently.

sametz commented 11 months ago

A test in Jupyter with %%timeit didn't show a real speed boost, but I think your solution is more elegant, so let's do this.