Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
code execution speed & memory usage?

In the context of Python/Quantopian, are there tools to measure code execution performance? In MATLAB, for example, there are the tic & toc functions (along with other tools here). Anything similar in Python that we could use? If so, some examples would be helpful.

8 responses

A very easy way in the tic/toc style is this:

t = time.time()  
# do stuff  
elapsed = time.time() - t  

Maybe the average execution speed of handle_data and batch_transforms could be gathered by default though and displayed with the other results.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

+1 for the new metrics @twiecki. Maybe total, average, stddev, max, min?

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Yeah, that probably doesn't leave much to be desired. Memory profiling might be a little bit trickier but I suspect also less interesting (run time is affecting users, memory usage mostly Quantopian).

Thanks...the code above should do the trick for now. Thomas and I are still plugging away at the moving average implementation and at some point it'd be interesting (once we get it running) to see if we can speed it up. I suspect that the science/math types using Python have this all worked out, and we can just apply best-practices.

Just a comment on the memory usage. In MATLAB, as I understand, there is an interaction between execution speed and what chunks of memory are pre-allocated versus dynamically allocated, how computations are structured when addressing matrices, etc. The same probably applies to Python.

Yes, it does. However, I wouldn't worry about speed and memory too much at this point as there are other bottlenecks. Essentially, we haven't optimized our backtester for speed yet (although we did make some progress on that front recently). There are interesting avenues like compiling your algorithm with numba to speed up loops but at the moment that's just pie in the sky.

Seconding what Thomas said - don't worry too much about speed and memory yet. I'd think of it as premature optimization. We're still adding features, refactoring code, changing back end stuff, adding more security, all sorts of things. Anything you optimize too finely now is going to be disrupted over the next weeks/months. Beta software - great in some ways, frustrating in others!

That said, keeping an eye on gross optimizations is always a good idea.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

The code Thomas provides above requires:

import time  

Otherwise, I found it to work "out-of-the-box." Thanks!

As a side note, at some point, you'll need to provide guidance to and tools for users to ensure that their algorithms are executing fast enough. Can't do minutely trading if it takes more than a minute to figure out what to buy/sell!