Date: 2026-06-03 Author: Longxia (龙虾) AI Writing System Mojo Version: 0.26.2.0 (d627decc) Python Version: 3.10.12 Platform: WSL Ubuntu 22.04 on Windows 11
Mojo is a new programming language that claims to combine Python's usability with systems-level performance. Key promises include:
- Python syntax compatibility — write Python-like code with zero learning curve
- LLVM-based compilation — native machine code, no interpreter overhead
- Seamless Python interop — import and call any Python library directly
This benchmark tests these claims in a real bioinformatics context: FASTA file GC content calculation, one of the most fundamental operations in genomics.
GC content (%G+C in a DNA sequence) is universally used in:
- Genome assembly quality assessment
- Taxonomic classification of metagenomic reads
- Gene prediction and promoter identification
- Methylation analysis
- Species identification (e.g., bacterial GC% ranges from 25% to 75%)
A typical bioinformatics workflow processes FASTA files ranging from megabytes to gigabytes, making GC computation a genuine bottleneck in pipelines.
Both implementations perform the same core task:
- Read a FASTA file
- Parse sequence records (header lines starting with
>) - Compute GC content (percentage of G+C bases among A+T+G+C)
- Report global statistics (sequence count, total bases, GC%)
| Aspect | Python | Mojo |
|---|---|---|
| I/O | Built-in open() |
Mojo open() (native) |
| Parsing | Line-by-line with for line in fh |
Single-pass char scanning |
| GC counting | str.upper() + membership test |
Direct char comparison |
| Statistics | sum(), sorted(), math.sqrt() |
Inline arithmetic |
| String concatenation | "".join(parts) per sequence |
Streaming (no per-seq concat) |
| Timing | time.time() |
Python interop time.time() |
| CLI args | argparse |
Environment variable FASTA_FILE |
| Constraint | Workaround |
|---|---|
No let keyword |
Use var for all declarations |
str[i] indexing requires byte keyword |
Use for ch in str iteration |
DynamicVector not in std.collections |
Used simpler single-pass algorithm |
| PythonObject can't auto-convert to Mojo types | Explicit String(py_obj) conversion |
Variable name gc conflicts with built-in |
Renamed to total_gc_count |
sys.argv() returns only mojo file path |
Used env var FASTA_FILE instead |
| Chinese comments cause encoding issues | All comments in English |
fn main() must be raises if using Python interop |
Added raises annotation |
Three randomly generated FASTA files with varying GC biases:
| Size | Sequences | File Size | Description |
|---|---|---|---|
| Small | 1,000 | ~4 MB | Quick sanity check |
| Medium | 5,000 | ~20 MB | Typical transcriptome |
| Large | 20,000 | ~80 MB | Small genome |
Sequences have mixed GC content: 33% high-GC bias, 33% low-GC bias, 33% uniform.
- Each configuration run 3 times
- Report mean and standard deviation
- No warmup runs (cold start reflects real CLI tool usage)
- Both programs run on same WSL instance, same file
| Run | Python (s) | Mojo (s) | Speedup |
|---|---|---|---|
| 1 | 0.5477 | 0.0940 | 5.8× |
| 2 | 0.5247 | 0.0856 | 6.1× |
| 3 | 0.5085 | 0.0863 | 5.9× |
| Mean | 0.5270 | 0.0886 | 5.9× |
| Run | Python (s) | Mojo (s) | Speedup |
|---|---|---|---|
| 1 | 5.5545 | 0.4199 | 13.2× |
| 2 | 2.6673 | 0.4413 | 6.0× |
| 3 | 2.8746 | 0.5467 | 5.3× |
| Mean | 3.6988 | 0.4693 | 7.9× |
Note: Python Run 1 for medium file shows a ~5.5s outlier, likely due to OS page cache cold miss. Runs 2-3 (~2.7-2.9s) are more representative.
| Run | Python (s) | Mojo (s) | Speedup |
|---|---|---|---|
| 1 | 10.8143 | 1.9020 | 5.7× |
| 2 | 13.5162 | 1.7649 | 7.7× |
| 3 | 10.5963 | 1.8675 | 5.7× |
| Mean | 11.6423 | 1.8448 | 6.3× |
| File Size | Sequences | Python (s) | Mojo (s) | Speedup |
|---|---|---|---|---|
| ~4 MB | 1,000 | 0.53 | 0.09 | 5.9× |
| ~20 MB | 5,000 | 3.70 | 0.47 | 7.9× |
| ~80 MB | 20,000 | 11.64 | 1.84 | 6.3× |
Both implementations produce consistent results:
| File | Python GC% | Mojo GC% | Difference |
|---|---|---|---|
| Small | 49.85% | 49.84% | <0.01% |
| Medium | 50.40% | 50.39% | <0.01% |
| Large | 49.91% | 49.90% | <0.01% |
Minor differences are due to slightly different counting strategies (Python counts per-sequence then sums; Mojo does a single pass), but results agree to 3+ decimal places.
-
No interpreter overhead: Mojo compiles to native code via LLVM. Python must interpret each bytecode instruction, dispatch through CPython's eval loop.
-
Type specialization: Mojo's
IntandFloat64map directly to machine registers. Python'sintis an arbitrary-precision object (28+ bytes), andfloatis a boxeddouble. -
No boxing/unboxing: Python's
for ch in seqcreates a new string object per character. Mojo iterates over raw bytes with zero allocation. -
Compiler optimization: LLVM can inline
count_gcandcount_atgc, eliminate redundant checks, and vectorize the scanning loop. CPython's interpreter prevents such optimization. -
Single-pass algorithm: The Mojo version was written as a single-pass scanner (inspired by 0.26's limited collection support), which is inherently more cache-friendly than Python's line-splitting approach.
- Different algorithms: Python uses line-by-line parsing with per-sequence GC calculation; Mojo uses single-pass character scanning. The Mojo algorithm is simpler but functionally equivalent for global GC statistics.
- Python could be faster: Using
collections.Counteror numpy would change the comparison. This benchmark intentionally tests "idiomatic Python" vs "idiomatic Mojo". - Mojo uses Python interop for timing: The
from python import Pythonoverhead is included in Mojo's timing, which slightly penalizes Mojo. Real-world Mojo code would use native timing APIs when available.
| Aspect | Rating | Notes |
|---|---|---|
| Python familiarity | ⭐⭐⭐⭐ | Syntax is very close, easy to get started |
| Error messages | ⭐⭐ | Often cryptic, especially for type conversion |
| Python interop | ⭐⭐⭐ | Works for simple cases, but PythonObject ↔ Mojo type conversion is painful |
| Standard library | ⭐⭐ | Very limited: no DynamicVector, no argv, no native timing |
| Compilation speed | ⭐⭐⭐ | Acceptable for small files, but noticeable for iterative development |
| Documentation | ⭐⭐ | API docs are sparse, many features discovered by trial and error |
Good fit:
- CPU-bound string/sequence processing (GC counting, k-mer enumeration, motif scanning)
- Hot loops over large genomic datasets where Python's interpreter overhead dominates
- Scenarios where numpy/scipy are overkill or don't apply
- Projects that need Python library access + native performance
Not yet ready:
- Production pipelines requiring mature error handling and logging
- Code that relies heavily on complex Python data structures
- Collaborative projects where team members don't know Mojo
- Tasks where I/O dominates (disk/network bound, not CPU bound)
-
Mojo delivers 6-8× speedup over idiomatic Python for FASTA GC content calculation, consistent across file sizes from 4MB to 80MB.
-
The speedup is robust — not a microbenchmark trick. Both implementations do real work (file I/O, parsing, statistics), and the gap persists at scale.
-
Python interop works but has rough edges in Mojo 0.26. Type conversion between PythonObject and Mojo types requires explicit handling.
-
Mojo's standard library is immature. Basic functionality (dynamic collections, CLI argument parsing, timing) requires workarounds or Python interop. This will likely improve rapidly as the language matures.
-
For bioinformatics specifically, Mojo shows strong potential for accelerating sequence analysis pipelines. A 6× speedup on core operations like GC counting, k-mer analysis, or quality filtering could translate to significant time savings on large genomic datasets.
-
The "write Python, get C speed" promise is partially fulfilled. The syntax is familiar, but the current version requires understanding Mojo-specific constraints (no
let, limited stdlib, type conversion quirks) that add friction.
OS: Windows 11 + WSL Ubuntu 22.04
Python: 3.10.12 (system)
Mojo: 0.26.2.0 (d627decc)
CPU: [Detected from WSL]
# 1. Generate test data
python3 scripts/gen_test_data.py
# 2. Run Python benchmark
python3 python/gc_content.py /tmp/mojo_bench/test_small.fasta --summary-only
python3 python/gc_content.py /tmp/mojo_bench/test_medium.fasta --summary-only
python3 python/gc_content.py /tmp/mojo_bench/test_large.fasta --summary-only
# 3. Run Mojo benchmark
FASTA_FILE=/tmp/mojo_bench/test_small.fasta mojo mojo/gc_content.mojo
FASTA_FILE=/tmp/mojo_bench/test_medium.fasta mojo mojo/gc_content.mojo
FASTA_FILE=/tmp/mojo_bench/test_large.fasta mojo mojo/gc_content.mojomojo-vs-python-gc/
├── README.md # This report
├── python/
│ └── gc_content.py # Python implementation
├── mojo/
│ └── gc_content.mojo # Mojo implementation
├── scripts/
│ └── gen_test_data.py # Test data generator
└── results/
└── benchmark_data.csv # Raw benchmark data
- Mojo compiled binary: Test
mojo buildfor AOT-compiled binary (should be even faster than JIT) - Parallel processing: Mojo supports
parallelize()— test multi-threaded GC computation - Larger datasets: Test on real genomic FASTA (e.g., human chr1, ~250MB)
- More complex bioinformatics tools: Extend to k-mer counting, FASTQ quality filtering, or sequence alignment
- Mojo SDK updates: Re-benchmark when Mojo reaches 1.0 with mature stdlib
Generated by 龙虾 (Longxia) AI Writing System on 2026-06-03