Liking cljdoc? Tell your friends :D

MSM Time Series Benchmark Analysis

Overview

This document summarizes benchmark results for the MSM (Move-Split-Merge) metric implementation, including the direct DP algorithm, lower-bound pruning, and hybrid trie-based search.

Test Environment:

  • Series length: 50 elements (unless otherwise noted)
  • Values: Random floats in [0, 100]
  • MSM cost parameter: c = 1.0
  • Threshold: 50.0 (where applicable)

1. MSM DP Algorithm Performance

Series LengthStandard DPOptimized DPSpeedup
10~565 ns~565 ns1.0×
50~19 µs~14 µs1.4×
100~77 µs~56 µs1.4×
500~2.94 ms~2.12 ms1.4×

Key Findings:

  • The optimized DP (using O(n) space instead of O(mn)) provides ~40% speedup
  • Complexity: O(mn) time, O(min(m,n)) space for optimized variant
  • Throughput: ~168-238 Kelem/s for 500-element series

2. Lower Bound Performance

Series LengthLength LBEuclidean LBL1 LBCombined LB
102.0 ns11 ns10 ns12 ns
502.0 ns40 ns35 ns42 ns
1002.0 ns80 ns75 ns107 ns
5002.0 ns424 ns400 ns446 ns

Key Findings:

  • Length LB is O(1) at ~2 ns regardless of series length
  • Euclidean/L1 LBs are O(n) with excellent cache locality
  • Combined LB computes max of all bounds for tightest pruning
  • Speedup vs Full MSM: 4,700-5,000× faster for 500-element series

3. Lower Bound Effectiveness in Hybrid Search

LB Type Comparison (500 series database, 50-element series)

LB TypeSearch TimeRelative
Length Only143.66 ms1.00×
Euclidean136.69 ms0.95×
L1135.23 ms0.94×
Combined136.12 ms0.95×

Key Findings:

  • Euclidean and L1 bounds provide tighter pruning than length-only
  • Combined LB adds slight overhead but provides most aggressive pruning
  • For random data with threshold=50.0, ~5-6% improvement from tighter LBs

With/Without LB Pruning

Database SizeWith LBWithout LBSpeedup
100 series25.7 ms31.2 ms1.21×
500 series145.9 ms154.1 ms1.06×
1000 series287.9 ms312.6 ms1.09×

Key Findings:

  • LB pruning provides 6-21% speedup depending on database size
  • Greater relative benefit for smaller databases (lower trie overhead ratio)
  • LB pruning becomes more valuable as MSM threshold tightens

4. Brute Force vs Indexed Search

Database SizeBrute Force + LBHybrid IndexRatio
100 series6.7 µs26.6 ms1:4000
500 series35.6 µs139.1 ms1:3900

Analysis:

This surprising result requires careful interpretation:

  1. Brute Force with LB only computes lower bounds in the fast path. When the LB exceeds the threshold (most candidates for tight thresholds), no full MSM is computed.

  2. Hybrid Index incurs overhead from:

    • Trie traversal using Levenshtein automaton
    • Quantization encoding/decoding
    • More candidates passing trie filter (approximate)
    • Full MSM verification for each candidate
  3. When Brute Force Wins:

    • Small databases (< 1,000 series)
    • Tight thresholds (LB prunes most candidates)
    • No prefix sharing in data (random series)
  4. When Hybrid Index Wins:

    • Large databases with shared prefixes
    • Loose thresholds where many candidates need verification
    • Repeated queries amortize index construction cost
    • Approximate search sufficient (skip MSM verification)

Recommendation: Use brute-force with LB pruning for databases under 10,000 series. Consider hybrid indexing for larger databases or when prefix sharing is expected.

5. Quantization Level Impact

Bins (K)Search TimeRelative
1610.8 ms1.0×
6484.5 ms7.8×
25685.1 ms7.9×

Key Findings:

  • Coarser quantization (16 bins) is ~8× faster due to smaller alphabet
  • 64 and 256 bins have similar performance (automaton overhead dominates)
  • Trade-off: Fewer bins = faster search but more false positives

Recommendation: Start with K=64 bins for balanced precision/performance. Use K=16 for approximate search where speed is critical.

6. Scaling Analysis

Time Complexity

OperationComplexityNotes
MSM DPO(mn)m, n = series lengths
Length LBO(1)Just length comparison
Euclidean LBO(min(m,n))Single pass, SIMD-friendly
Trie SearchO(m × k^d)m = query length, k = alphabet, d = max distance
Hybrid SearchO(m × k^d + c × mn)c = candidate count after pruning

Space Complexity

StructureSpaceNotes
MSM DPO(min(m,n))Optimized variant
Trie IndexO(N × L)N = series count, L = avg length, with prefix sharing
Hybrid IndexO(N × L + N × L')Additional storage for original floats

7. Recommendations

Small Databases (< 1,000 series)

// Use brute force with lower bounds
use liblevenshtein::time_series::{search_with_lb, MsmConfig};

let results = search_with_lb(&query, &database, threshold, &msm_config);

Medium Databases (1,000 - 100,000 series)

// Use hybrid search with euclidean lower bounds
use liblevenshtein::time_series::{HybridSearchIndex, QuantizationConfig, MsmConfig, LowerBoundType};

let mut index = HybridSearchIndex::new(quant_config, msm_config);
index.set_lower_bound_type(LowerBoundType::EuclideanOnly);
// ... insert series ...
let results = index.search_exact(&query, threshold);

Large Databases (> 100,000 series)

// Use approximate search with verification on top-k
let candidates = index.search_approximate(&query, max_distance);
// Verify top-k candidates with exact MSM

8. Future Optimizations

  1. SIMD-accelerated Euclidean LB - Use AVX2/SSE4 for parallel distance computation
  2. Parallel LB pruning - Use Rayon for multi-threaded candidate filtering
  3. Adaptive LB selection - Choose LB type based on series characteristics
  4. Tighter lower bounds - Implement LB_Keogh or envelope-based bounds
  5. Batch queries - Amortize index traversal across multiple queries

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close