FSharp runs my algorithm slower than Python
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Both F# and Python are popular programming languages that developers use to implement algorithms. However, it's not uncommon to encounter performance variations between these languages when executing the same algorithm. This article explores why F# might run an algorithm slower than Python, taking into account various technical factors, examples, and performance benchmarks.
Language Characteristics
F# Characteristics
- Compiled Language: F# is a statically-typed, functional-first language running on the .NET runtime. Being a compiled language, F# should theoretically have performance advantages because the compilation step translates F# code to efficient machine code.
- Functional Paradigm: F# is heavily functional with strong emphasis on immutability and first-class functions, which can occasionally lead to performance overhead due to functional data structures.
- Concurrency: F# offers robust concurrency models using Asynchronous Workflows, which can be a double-edged sword in terms of performance.
Python Characteristics
- Interpreted Language: Python is an interpreted, dynamically-typed language, generally leading to slower execution in raw computation tasks compared to compiled languages. However, the use of performant libraries can mitigate this.
- Dynamic Typing: Python’s lack of static type checks offers flexibility at the cost of potential inefficiencies.
- Libraries and Extensions: Python's vast ecosystem of libraries, especially C-extensions like NumPy, offer high-performance computation capabilities.
Factors Affecting Performance
Memory Usage
- Garbage Collection: F#'s garbage collector works within the .NET framework, which might lead to irregular pauses affecting performance. Python also uses garbage collection, but its reference counting mechanism releases memory more predictably.
- Data Structure Overhead: F#'s functional data structures can incur overhead from immutability and persistent data structures, slowing down some operations. Python's mutable structures might be more efficient in this regard.
Optimizations
- Just-In-Time Compilation: .NET uses JIT compilation which at times optimizes F# code well. Python can leverage the PyPy interpreter for JIT benefits, potentially running some algorithms faster.
- Looping Constructs: Python has highly optimized loops and list comprehensions, especially if the algorithms leverage C-extensions.
Example Algorithm: Merge Sort
F# Implementation
Related reading
- FTRL implementation in tensorflow V.S. FTRL in Google's research paper
- Fuel chart smoothing algorithm
- Function to make a list as unsorted as possible
- Fuzzy date algorithm
- Fulltext Search with InnoDB
- Fully cover a rectangle with minimum amount of fixed radius circles
- FutureWarning arrays to stack must be passed as a sequence type such as list or tuple. Support for non-sequence iterables is deprecated
- FutureWarning Conversion of the second argument of issubdtype from float to np.floating is deprecated

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.