𝗜 𝗳𝗼𝘂𝗻𝗱 𝗮 𝟰𝟰× 𝘀𝗽𝗲𝗲𝗱 𝗱𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝗰𝗲 𝗶𝗻 𝗮 𝘀𝗶𝗺𝗽𝗹𝗲 𝘀𝘂𝗺() 𝗼𝗽𝗲𝗿𝗮𝘁𝗶𝗼𝗻 🚀 I benchmarked three ways of summing 100,000 numbers: • Manual for loop → ~11.4 ms • Built-in 𝘀𝘂𝗺() → ~8.27 ms • 𝗻𝗽.𝘀𝘂𝗺() → ~0.259 ms 𝗡𝘂𝗺𝗣𝘆 𝘄𝗮𝘀 ~𝟰𝟰× 𝗳𝗮𝘀𝘁𝗲𝗿 𝘁𝗵𝗮𝗻 𝗮 𝗣𝘆𝘁𝗵𝗼𝗻 𝗹𝗼𝗼𝗽 ⚡ The real insight isn’t that “NumPy is faster.” It’s about execution layers. A Python loop runs inside the interpreter with dynamic checks every iteration. 𝘀𝘂𝗺() shifts the work into C. 𝗻𝗽.𝘀𝘂𝗺() operates on contiguous memory using optimized low-level code, avoiding Python-level iteration entirely. Same computation. Different execution layer. Massive performance gap. #Python #NumPy #DataScience #LearningInPublic
NumPy speeds up Python loops with optimized execution
More Relevant Posts
-
Why can’t we just search embeddings with a Python script instead of using a vector database? While working with RAG systems, this question came to mind. A simple approach would be to compare a query embedding with every vector using linear search. But that takes O(n) time — which becomes very slow when you have millions of vectors. Vector databases solve this using Approximate Nearest Neighbor (ANN) algorithms like HNSW and LSH. These structures allow fast similarity search by navigating the vector space efficiently instead of checking every vector. That’s why most modern RAG pipelines rely on vector databases. Interesting how much engineering goes into making retrieval fast. #MachineLearning #GenAI #RAG #VectorDB
To view or add a comment, sign in
-
LeetCode 23: Merge k sorted list: You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. Merge all the linked-lists into one sorted linked-list and return it. Approach: Simple and straightforward, iterate through each of the linked-list in the given list, push each node's value into a priority queue using heapq module, create a new linked-list using the elements of priority queue. Time complexity: O(n logn) where n is the total number of values. Space complexity: O(n) #Python #LeetCode #DSA #CompetitiveProgramming #DataStructures #Algorithms #ProblemSolving
To view or add a comment, sign in
-
-
LeetCode Problem 1545: "Given two positive integers n and k, the binary string Sn is formed as follows: S1 = "0" Si = Si - 1 + "1" + reverse(invert(Si - 1)) for i > 1 Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0)." Approach: Initialize an array with a single element '0' which serves as the string1 then iteratively calculate the consecutive strings and store them in the array. Finally, form the final string using the join() function and return the kth character of the string. #Python #LeetCode #String #ProblemSolving #CompetitiveProgramming #DataStructures #Algorithm
To view or add a comment, sign in
-
-
LeetCode Problem 1582: "Special Positions in a Binary Matrix": Given an m x n binary matrix mat, return the number of special positions in mat. A position (i, j) is called special if mat[i][j] == 1 and all other elements in row i and column j are 0 (rows and columns are 0-indexed). Approach: Initialize two arrays, one stores the number of 1s in each row of the matrix and other stores the number of 1s in each column, by traversing the matrix for filling each of the arrays. Now re-traverse the matrix and if mat[i][j]==1, check for the number of 1s in corresponding row and column, if number of 1s in that row and column is equal to one, we get our required position, calculate the total number of such positions. #Python #LeetCode #Matrices #ProblemSolving #CompetitiveProgramming #DataStructures #Algorithms #Arrays #Coding
To view or add a comment, sign in
-
-
LeetCode Problem 1758: "Minimum changes to make alternating binary string": You are given a string s consisting only of the characters '0' and '1'. In one operation, you can change any '0' to '1' or vice versa. The string is called alternating if no two adjacent characters are equal. For example, the string "010" is alternating, while the string "0100" is not. Return the minimum number of operations needed to make s alternating. Approach: Here are two possibilities, first do not make change in the 1st character and apply rules following it, second consider step of changing the very first character and apply rules following it. The below implementation in Python works on this principle only, simple and straightforward. #Python #LeetCode #Strings #OptimalSolution #CompetitiveProgramming #DataStructures #Algorithms #ProblemSolving
To view or add a comment, sign in
-
-
✅ Day 64 of 100 Days LeetCode Challenge Problem: 🔹 #1784 – Check if Binary String Has at Most One Segment of Ones 🔗 https://lnkd.in/gpJYed9J Learning Journey: 🔹 Today’s problem focused on determining whether a binary string contains only one contiguous segment of '1's. 🔹 Since the string has no leading zeros, the first character is always '1'. 🔹 If a '1' appears again after a '0', it indicates the start of a second segment. 🔹 A single pass through the string is enough to detect this pattern. Concepts Used: 🔹 String Traversal 🔹 Pattern Detection 🔹 Conditional Logic Key Insight: 🔹 The pattern "01" followed by another '1' reveals multiple segments of ones. 🔹 Detecting this transition allows us to solve the problem efficiently. Complexity: 🔹 Time: O(n) 🔹 Space: O(1) 💬 What’s your favorite trick for solving binary string problems? #LeetCode #Algorithms #DataStructures #CodingInterview #100DaysOfCode #SoftwareEngineering #Python #ProblemSolving #LearningInPublic #TechCareers
To view or add a comment, sign in
-
-
Most people use scipy.optimize and move on. I wanted to understand what's actually happening underneath. So I built three optimization algorithms from scratch in Python, no external solvers, just NumPy: → Truncated Newton: finds local minima efficiently, approximating the Hessian without ever computing it fully → Sequential Penalty: handles constraints by turning them into a penalty, the more you violate them, the harder the algorithm pushes back → Filled Functions: the interesting one. When you're stuck in a local minimum, it builds an artificial "hill" on top of it to force the search elsewhere, that's how you find the global optimum Tested on 20+ benchmark problems. It works. In collaboration with Paolo Pascarelli and Silvia Alonzo Full code + writeup below 👇 https://lnkd.in/d8TwHtcS #python #optimization #numericalcomputing
To view or add a comment, sign in
-
🚀 Solved the “Two Sum” Problem | Data Structures & Algorithms Practice Today I solved the classic Two Sum problem—a fundamental question in data structures & algorithms. 🔹 Problem: 1 Given an array of integers and a target value, return the indices of two numbers such that they add up to the target. ⏱️ Complexity: Time Complexity: O(n) Space Complexity: O(n) 🔗 GitHub Repository (more DSA problems inside): https://lnkd.in/gdrbnQDF #DSA #ProblemSolving #Python #CodingJourney #SoftwareEngineering #LeetCode
To view or add a comment, sign in
-
-
Imagine trying to explain a complex 3D object using just one photo. Pick the wrong angle, and you might lose critical detail. The same thing happens when you try to simplify high-dimensional data. If you just drop a random axis to make it 2D, you could throw away crucial information and end up with a tangled mess. Enter Principal Component Analysis (PCA)! Instead of randomly dropping data, PCA rotates your entire coordinate system to find the absolute "best camera angle". Watch our quick 60-second visual breakdown below! 👇 if you want to dive deeper into the math behind the magic and get the Python code, Watch the full tutorial here: https://lnkd.in/gdGkEw8r #PCA #MachineLearning #DataScience #DataVisualization #Schovia #Shorts
To view or add a comment, sign in
-
Don't flatten what naturally has structure. It's tempting to model everything in a single class. Easy to write, easy to read, at least until your data grows. This is where most codebases start, with just one model. But with model composition, each model has a single responsibility. And Pydantic handles nested validation automatically. Structure your models the way your domain is actually structured. The code gets cleaner, the errors get clearer, and reuse becomes obvious. This and other real-world modelling patterns are covered in Practical Pydantic: 👉 https://lnkd.in/eGiB7ZxU Model your domain. Not just your data. #Python #Pydantic #Data #Models #Patterns
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
Great breakdown, Divyansh Sharma! When I was first learning NumPy, it was often emphasized that it is much faster than standard methods, but your post makes that concept truly practical through your benchmarking. Seeing the performance gap explained through execution layers—shifting from Python loops to C and then to contiguous memory operations—really clarifies the 'why' behind the speed. Thanks for sharing this insightful analysis.