I love Python. But it's slow. So I wrote C code to value an option and call it from Python. C helps Python run up to 45X faster. Here's how: Before you ask, yes I know there are better ways to do this. Here's the problem: A lot of quant code is already written in C. So instead of re-writing it from scratch, most quants wrap existing code in Python. Enjoy: https://lnkd.in/gDnfmtGk
Boosting Python Performance with C Code
More Relevant Posts
-
I love Python. But it's slow. So I wrote C code to value an option and call it from Python. C helps Python run up to 45X faster. Here's how: Before you ask, yes I know there are better ways to do this. Here's the problem: A lot of quant code is already written in C. So instead of re-writing it from scratch, most quants wrap existing code in Python. Enjoy: https://lnkd.in/ejFiHXdy
To view or add a comment, sign in
-
Ever wonder how much memory an empty list takes? How about how long it takes to add two integers in Python? How fast is adding an element to a Python list? How does that compare to opening a file does it usually take less than a millisecond? Are there hidden factors that make these operations slower than expected? When writing performance-sensitive code, which data structures are most appropriate? How much memory does a floating-point number consume in Python? What about a single character or an empty string? Came across a great write-up on this👇 https://lnkd.in/gdWieZhY
To view or add a comment, sign in
-
Why range(1,000,000) is cheap, but list(range(1,000,000)) is costly in Python? TL;DR: Iteration Protocol in Python needs to know only next item and not full list. The "Next Page" Rule Iteration in Python isn't about having a collection of items; it’s about knowing how to get the next item. Two special methods make this possible: 1. __iter__() → tells Python “I can be looped over” 2. __next__() → returns the next value, one at a time When there’s nothing left, StopIteration tells Python to stop the loop. Why this matters? When we use a list, we pay for all the memory upfront. When we use the Iteration Protocol, we only pay for one item at a time. This is called Lazy Evaluation. Takeaway - If the object represents a collection or a stream of data, implement __iter__ and __next__. It makes the code more memory-efficient and much more "Pythonic." I’m deep-diving into the Python protocols this week and will share my learnings. Do follow along and tell your experiences in comments. #Python #PythonInternals #SoftwareEngineering #BackendDevelopment
To view or add a comment, sign in
-
-
How does len() function knows how to handle a List, a String, and a Dictionary all the same way in Python? TL;DR: Python Protocols A Protocol is just the set of rules (special methods) an object follows. "Duck Typing" Contract In Python does not care about what the object is, it cares only about what it does. These are represented as dunders (__method__) in Python. 3 Protocols we use everyday in Python: -> Sized (__len__): Tells Python that object has a size. (Powers len()) -> Container (__contains__): Tells Python how to check if something is "inside" the object. (Powers the in keyword) -> Iterable (__iter__): Tells Python that object can be looped over. (Powers for x in obj) Why is this useful? As a developer, we want our code to be intuitive. By following these protocols, we make our custom objects compatible with all of Python’s built-in tools. Protocols are the API of the Python language itself. Using them make custom objects start feeling like native Python parts. I’m deep-diving into the Python protocols this week and will share my learnings. Do follow along and tell your experiences in comments. #Python #PythonInternals #SoftwareEngineering #BackendDevelopment
To view or add a comment, sign in
-
-
Many Python beginners struggle not because of syntax, but because of missing concepts. I just published a new article on 12 Python Concepts That Matter More Than Syntax It explains: ✔️ How Python actually works ✔️ Why beginners get stuck ✔️ What data analysts really need If you’re learning Python for data, this will change how you learn. Read it here : https://lnkd.in/dy6y3rjQ
To view or add a comment, sign in
-
This is probably the best and simplest explanation I've seen out there on how to use uv. If you've gone around in circles messing with your dependencies and versioning issues for your python packages, or if you're tired of writing source venv/bin/activate, look no further.
If you're a Python user and haven't tried uv, you're missing out! I just wrote an article to help you get started with uv. Read it here: https://lnkd.in/gZVGRA3f #data #python #uv
To view or add a comment, sign in
-
I’ve worked with Python for a few years, and variables are at the core of every program. I knew the rules like using deepcopy when copying a list but for a long time, the why behind them eluded me. Recently, I decided to take a deeper dive into Python’s internals, and everything started to click. That exploration inspired me to write a short article explaining how variables really work and why those rules exist. If you’ve ever wondered why Python behaves the way it does, I hope this is useful. https://lnkd.in/e9xNMmhf
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development
Thanks ! A lot to read today 😅