Skip to content
ry

Benchmarks

Four suites compare ry’s runtime with GNU R. Two run code that others wrote: the programs that R compiler research measures itself on, and the test suites of CRAN packages. The other two run code we wrote: small numeric kernels, each also written in typed ry, and programs in the shape of package and business code. A fifth section times the three benchmark code bases, outside this summary. Each suite’s geometric mean of ry’s speed-ups over GNU R:

SuiteWhat it isry vs GNU Rry typed
Public R JIT benchmarks54 standard programs from R compiler research (Ř, rcp)3.76x20x
Package test suites26 pinned CRAN packages, each running its own test suite2.75xnot typed
Numeric kernels (ours)16 small programs we wrote whose time goes into loops, recursion and arithmetic on numbers6.06x26x
Application workloads (ours)15 programs we wrote in the shape of package and business code: calls, dispatch, conditions, records, tables3.77x72x

Geometric means of the speed-ups over GNU R; above 1x means ry is faster.

  • Public R JIT benchmarks: measured on 2026-11-16 on the ry 0.5.0 release archive (sha256 575ad8ad890b) with GNU R 4.6.2 on Intel(R) Xeon(R) Processor @ 2.10GHz, 4 cores, Linux 6.18.44-fc-v470; 10 paired rounds.
  • Package test suites: measured on 2026-11-16 on the ry 0.5.0 release archive (sha256 575ad8ad890b) with GNU R 4.6.2 on Intel(R) Xeon(R) Processor @ 2.10GHz, 4 cores, Linux 6.18.44-fc-v509; 10 paired rounds.
  • Benchmark code bases: measured on 2026-11-17 on the ry 0.5.0 release archive (sha256 575ad8ad890b) with GNU R 4.6.2 on Intel(R) Xeon(R) Processor @ 2.10GHz, 4 cores, Linux 6.18.44-fc-v553; 10 paired rounds.
  • Numeric kernels (ours): measured on 2026-11-17 on the ry 0.5.0 release archive (sha256 575ad8ad890b) with GNU R 4.6.2 on Intel(R) Xeon(R) Processor @ 2.10GHz, 4 cores, Linux 6.18.44-fc-v509; 10 paired rounds.
  • Application workloads (ours): measured on 2026-11-17 on the ry 0.5.0 release archive (sha256 575ad8ad890b) with GNU R 4.6.2 on Intel(R) Xeon(R) Processor @ 2.10GHz, 4 cores, Linux 6.18.44-fc-v509; 10 paired rounds.

Every number on this page is rendered from the results files in tools/bench/, and each chart names the date, the version of the engine ry is compared with, the machine and the ry commit of its measurement.

These are the programs that the Ř (rir) and rcp R compilers are measured on. They come from the benchmark directory of the r-compile-server repository, the suite of Kocourek, Křikava and Vitek, “Copy-and-Patch Just-in-Time Compiler for R” (VMIL 2025): all 57 but three that cannot be sized to a few seconds. The suite combines ports of Are We Fast Yet, ports of the Benchmarks Game and the rcp micro-benchmarks. They are vendored unchanged with their MIT licence, and each file ends with a driver that runs the program once at a fixed size and prints a checksum and its time. The sizes make GNU R 4.6 take about half a second to two seconds per program. Every run is a fresh process, so the time includes GNU R’s byte compilation and ry’s JIT tier-up, though not start-up.

Public R JIT benchmarks: Are We Fast Yet
  • ry
  • ry typed
Mandelbrot set, scalar loopsmandelbrottoo short on ry typedtoo short on ry typed, AOTry: 3.57xry typed: 22x
Tree allocationstoragetoo short on ry typedtoo short on ry typed, AOTry: 3.15xry typed: 44x
Bouncing balls, positional fields, simple setupbounce_nonames_simpletoo short on ry typedtoo short on ry typed, AOTry: 2.61xry typed: 23x
Bouncing balls, positional fieldsbounce_nonamestoo short on ry typedtoo short on ry typed, AOTry: 2.58xry typed: 22x
Bouncing balls, named fieldsbouncetoo short on ry typedtoo short on ry typed, AOTry: 2.24xry typed: 72x

Speed relative to GNU R on a log scale: a bar to the right of 1x is faster than GNU R, to the left slower. The line across a bar spans its 95% interval. A time marked too short on a ry engine is under 0.1 s. That speed is what the suite measures, so it counts in the geometric mean.

Measured on 2026-11-16 on the ry 0.5.0 release archive (sha256 575ad8ad890b) with GNU R 4.6.2 on Intel(R) Xeon(R) Processor @ 2.10GHz, 4 cores, Linux 6.18.44-fc-v470; 10 paired rounds.

Public R JIT benchmarks: Benchmarks Game
  • ry
  • ry typed
Spectral norm, scalar loopsspectralnorm_naivetoo short on rytoo short on ry typedtoo short on ry typed, AOTwide interval on rywide interval on ry typed, AOTry: 23xry typed: 63x
Spectral normspectralnormtoo short on rytoo short on ry typedtoo short on ry typed, AOTwide interval on ryry: 21xry typed: 50x
Reverse complement, whole filereversecomplement_2too short on ry typedtoo short on ry typed, AOTry: 9.88xry typed: 37x
Reverse complementreversecomplementtoo short on ry typedtoo short on ry typed, AOTry: 7.42xry typed: 31x
Reverse complement, scalar lookupreversecomplement_naivetoo short on ry typedtoo short on ry typed, AOTry: 5.87xry typed: 55x
K-nucleotide, named vector countsknucleotide_brute_3ry: 5.52xry typed: 4.76x
Fasta-reduxfastareduxtoo short on ry typedtoo short on ry typed, AOTry: 4.72xry typed: 26x
Fasta, scalar linear searchfasta_naivetoo short on ry typedtoo short on ry typed, AOTry: 4.53xry typed: 33x
Fannkuch-redux, scalar swapsfannkuchredux_naivetoo short on ry typedtoo short on ry typed, AOTwide interval on ry typed, AOTry: 4.37xry typed: 26x
K-nucleotide, attribute countsknucleotide_brutery: 4.17xry typed: 4.49x
Fasta-redux, scalar lookupfastaredux_naivetoo short on ry typedtoo short on ry typed, AOTwide interval on rywide interval on ry typedry: 4.07xry typed: 39x
Fasta, vectorized binary searchfasta_3too short on ry typedtoo short on ry typed, AOTry: 3.93xry typed: 17x
Fasta, vectorized linear searchfasta_2too short on ry typedtoo short on ry typed, AOTry: 3.86xry typed: 13x
K-nucleotide, hashed environmentknucleotidetoo short on ry typedtoo short on ry typed, AOTry: 3.76xry typed: 28x
Fastafastatoo short on ry typedtoo short on ry typed, AOTry: 3.43xry typed: 21x
Fasta, scalar binary searchfasta_naive_2too short on ry typedtoo short on ry typed, AOTry: 3.40xry typed: 41x
Regex DNAregexdnary: 3.07xry typed: 0.97x
Spectral norm, matrix rebuilt per productspectralnorm_alt_2ry: 3.05xry typed: 2.32x
K-nucleotide, attribute counts, growing vectorknucleotide_brute_2ry: 3.00xry typed: 4.39x
Pi digitspidigitstoo short on ry typedtoo short on ry typed, AOTry: 2.78xry typed: 16x
Mandelbrot, scalar pointsmandelbrot_naive_asciitoo short on ry typedtoo short on ry typed, AOTry: 2.71xry typed: 69x
Spectral norm, element-wise matrix fillspectralnorm_alt_3too short on ry typedtoo short on ry typed, AOTwide interval on ry typed, AOTry: 2.60xry typed: 37x
Spectral norm, precomputed matrixspectralnorm_altry: 2.14xry typed: 1.83x
Binary treesbinarytreestoo short on ry typedtoo short on ry typed, AOTry: 1.87xry typed: 53x
N-bodynbodytoo short on ry typedtoo short on ry typed, AOTry: 1.87xry typed: 75x
N-body, matrix statenbody_2ry: 1.82xry typed: 14x
Binary trees, inline arithmeticbinarytrees_2too short on ry typedtoo short on ry typed, AOTry: 1.70xry typed: 37x
Binary trees, explicit loopbinarytrees_naivetoo short on ry typedtoo short on ry typed, AOTry: 1.67xry typed: 54x
Fannkuch-reduxfannkuchreduxry: 1.66xry typed: 20x
Mandelbrot, vectorized rows, no outputmandelbrot_nooutry: 1.60xry typed: 1.58x
Mandelbrot, vectorized rowsmandelbrot_asciiwide interval on ry typedry: 1.58xry typed: 1.64x
Mandelbrot, whole-plane matrixmandelbrot_noout_naivery: 1.36xry typed: 2.15x
Spectral norm, eigenvaluesspectralnorm_mathry: 1.16xry typed: 0.74x

Speed relative to GNU R on a log scale: a bar to the right of 1x is faster than GNU R, to the left slower. The line across a bar spans its 95% interval. A time marked too short on a ry engine is under 0.1 s. That speed is what the suite measures, so it counts in the geometric mean. A program marked wide interval varied so much between rounds that its ratio's interval is wide; read its median with care.

Measured on 2026-11-16 on the ry 0.5.0 release archive (sha256 575ad8ad890b) with GNU R 4.6.2 on Intel(R) Xeon(R) Processor @ 2.10GHz, 4 cores, Linux 6.18.44-fc-v470; 10 paired rounds.

Public R JIT benchmarks: rcp micro-benchmarks and convolutions
  • ry
  • ry typed
Scalar assignment, while loopscalar-whiletoo short on ry typedtoo short on ry typed, AOTry: 14xry typed: 21x
Scalar assignment, for loopscalar-fortoo short on ry typedtoo short on ry typed, AOTwide interval on ry typedry: 12xry typed: 42x
Many local assignmentsbytecodestoo short on rytoo short on ry typedtoo short on ry typed, AOTwide interval on ry typed, AOTry: 10xry typed: 189x
Counting while loopempty-whiletoo short on rytoo short on ry typedtoo short on ry typed, AOTwide interval on ry typedry: 8.20xry typed: 30x
Convolution, scalar loopsconvolutiontoo short on ry typedtoo short on ry typed, AOTwide interval on ryry: 7.40xry typed: 25x
Vector element assignment, while loopvector-whilery: 6.90xry typed: 8.21x
Convolution, [[ indexingconvolution_slowtoo short on ry typedtoo short on ry typed, AOTry: 5.24xry typed: 34x
Growing a list, for looplist-fortoo short on ry typedtoo short on ry typed, AOTry: 5.17xry typed: 102x
Growing a list, while looplist-whiletoo short on ry typedtoo short on ry typed, AOTwide interval on ry typed, AOTry: 5.09xry typed: 98x
Superassignment loopsuper-whiletoo short on ry typedtoo short on ry typed, AOTwide interval on ry typedry: 4.73xry typed: 297x
Empty for loopempty-fortoo short on ry typedtoo short on ry typed, AOTry: 4.11xry typed: 10x
Vector element assignment, for loopvector-forry: 3.51xry typed: 7.89x
lapply with a closurelapplytoo short on ry typedtoo short on ry typed, AOTry: 3.47xry typed: 61x
lapply with an extra argumentlapply-dotstoo short on ry typedtoo short on ry typed, AOTry: 3.33xry typed: 75x
Matrix element assignmentmatrix-fortoo short on ry typedtoo short on ry typed, AOTry: 3.26xry typed: 12x
Convolution, vectorizedconvolution_vry: 1.60xry typed: 5.55x
geometric mean of all groupsry: 3.76xry typed: 20x

Speed relative to GNU R on a log scale: a bar to the right of 1x is faster than GNU R, to the left slower. The line across a bar spans its 95% interval. A time marked too short on a ry engine is under 0.1 s. That speed is what the suite measures, so it counts in the geometric mean. A program marked wide interval varied so much between rounds that its ratio's interval is wide; read its median with care.

Measured on 2026-11-16 on the ry 0.5.0 release archive (sha256 575ad8ad890b) with GNU R 4.6.2 on Intel(R) Xeon(R) Processor @ 2.10GHz, 4 cores, Linux 6.18.44-fc-v470; 10 paired rounds.

Scalar loops gain the most, because the JIT compiles them to machine code while GNU R interprets bytecode. Programs that spend their time allocating gain the least.

Each distinct algorithm also exists in typed ry, as a <name>.typed.R beside its main program, and runs in strict mode as the ry typed column; the variants of one algorithm have no typed version of their own. A port is written as typed ry is written, so where the R program relies on something typed ry does not have, such as ..., <<-, matrices or complex numbers, it uses the typed equivalent, and suite.toml notes which. Its printed output and result match GNU R’s, so it has the same checksum. awfy storage and regexdna have no typed version, because typed ry cannot yet express them as written.

The numbers: seconds and peak memory
programGNU Rryry typedry typed, AOTry, speed-upry typed, speed-upry typed, AOT, speed-up
Bouncing balls, named fields1.121 s0.490 s0.015 s0.015 s2.24x (2.10x to 2.40x)72x (68x to 76x) (too short on ry typed)72x (70x to 74x) (too short on ry typed, AOT)
Bouncing balls, positional fields1.095 s0.421 s0.051 s0.048 s2.58x (2.44x to 2.73x)22x (20x to 23x) (too short on ry typed)22x (21x to 23x) (too short on ry typed, AOT)
Bouncing balls, positional fields, simple setup1.127 s0.422 s0.049 s0.048 s2.61x (2.49x to 2.82x)23x (21x to 24x) (too short on ry typed)22x (22x to 24x) (too short on ry typed, AOT)
Mandelbrot set, scalar loops1.159 s0.326 s0.052 s0.052 s3.57x (3.36x to 3.72x)22x (21x to 23x) (too short on ry typed)22x (21x to 23x) (too short on ry typed, AOT)
Tree allocation1.239 s0.386 s0.028 s0.029 s3.15x (2.94x to 3.23x)44x (40x to 45x) (too short on ry typed)43x (40x to 45x) (too short on ry typed, AOT)
Binary trees1.784 s0.942 s0.033 s0.032 s1.87x (1.80x to 1.97x)53x (51x to 56x) (too short on ry typed)57x (54x to 58x) (too short on ry typed, AOT)
Binary trees, inline arithmetic0.685 s0.393 s0.018 s0.019 s1.70x (1.68x to 1.81x)37x (35x to 39x) (too short on ry typed)37x (33x to 38x) (too short on ry typed, AOT)
Binary trees, explicit loop1.683 s0.996 s0.031 s0.031 s1.67x (1.61x to 1.75x)54x (52x to 56x) (too short on ry typed)53x (51x to 55x) (too short on ry typed, AOT)
Fannkuch-redux2.766 s1.673 s0.143 s0.141 s1.66x (1.58x to 1.73x)20x (18x to 21x)20x (18x to 21x)
Fannkuch-redux, scalar swaps1.390 s0.314 s0.050 s0.053 s4.37x (4.04x to 4.56x)26x (24x to 28x) (too short on ry typed)25x (20x to 28x) (too short on ry typed, AOT, wide interval on ry typed, AOT)
Fasta0.959 s0.279 s0.047 s0.048 s3.43x (3.12x to 3.60x)21x (20x to 21x) (too short on ry typed)20x (19x to 20x) (too short on ry typed, AOT)
Fasta, vectorized linear search1.032 s0.268 s0.077 s0.073 s3.86x (3.60x to 4.02x)13x (12x to 13x) (too short on ry typed)14x (13x to 14x) (too short on ry typed, AOT)
Fasta, vectorized binary search1.262 s0.318 s0.077 s0.080 s3.93x (3.70x to 4.09x)17x (16x to 17x) (too short on ry typed)16x (15x to 16x) (too short on ry typed, AOT)
Fasta, scalar linear search1.500 s0.323 s0.044 s0.049 s4.53x (4.43x to 4.72x)33x (33x to 34x) (too short on ry typed)30x (29x to 31x) (too short on ry typed, AOT)
Fasta, scalar binary search2.100 s0.603 s0.051 s0.055 s3.40x (3.25x to 3.60x)41x (38x to 42x) (too short on ry typed)38x (37x to 39x) (too short on ry typed, AOT)
Fasta-redux1.206 s0.257 s0.046 s0.049 s4.72x (4.60x to 4.82x)26x (25x to 27x) (too short on ry typed)24x (22x to 25x) (too short on ry typed, AOT)
Fasta-redux, scalar lookup1.431 s0.346 s0.035 s0.037 s4.07x (3.63x to 4.22x) (wide interval on ry)39x (35x to 43x) (too short on ry typed, wide interval on ry typed)38x (37x to 39x) (too short on ry typed, AOT)
K-nucleotide, hashed environment1.245 s0.336 s0.044 s0.055 s3.76x (3.63x to 3.96x)28x (27x to 29x) (too short on ry typed)22x (21x to 23x) (too short on ry typed, AOT)
K-nucleotide, attribute counts0.834 s0.205 s0.183 s0.208 s4.17x (3.92x to 4.24x)4.49x (4.39x to 4.73x)4.10x (3.71x to 4.26x)
K-nucleotide, attribute counts, growing vector1.308 s0.435 s0.296 s0.310 s3.00x (2.98x to 3.06x)4.39x (4.19x to 4.51x)4.20x (4.06x to 4.36x)
K-nucleotide, named vector counts1.319 s0.237 s0.280 s0.278 s5.52x (5.15x to 6.04x)4.76x (4.38x to 4.93x)4.75x (4.63x to 5.21x)
Mandelbrot, vectorized rows0.939 s0.602 s0.577 s0.554 s1.58x (1.51x to 1.63x)1.64x (1.47x to 1.69x) (wide interval on ry typed)1.69x (1.61x to 1.75x)
Mandelbrot, scalar points1.682 s0.624 s0.024 s0.024 s2.71x (2.50x to 2.75x)69x (67x to 72x) (too short on ry typed)70x (66x to 72x) (too short on ry typed, AOT)
Mandelbrot, vectorized rows, no output0.734 s0.464 s0.467 s0.463 s1.60x (1.56x to 1.65x)1.58x (1.53x to 1.65x)1.54x (1.51x to 1.66x)
Mandelbrot, whole-plane matrix1.094 s0.811 s0.505 s0.517 s1.36x (1.27x to 1.40x)2.15x (2.07x to 2.22x)2.15x (2.05x to 2.22x)
N-body1.476 s0.782 s0.019 s0.020 s1.87x (1.81x to 1.97x)75x (71x to 78x) (too short on ry typed)74x (70x to 78x) (too short on ry typed, AOT)
N-body, matrix state1.480 s0.812 s0.102 s0.110 s1.82x (1.73x to 1.95x)14x (13x to 15x)13x (13x to 15x)
Pi digits0.811 s0.286 s0.050 s0.057 s2.78x (2.58x to 2.89x)16x (15x to 17x) (too short on ry typed)14x (14x to 15x) (too short on ry typed, AOT)
Regex DNA1.014 s0.331 s1.041 s1.047 s3.07x (2.96x to 3.33x)0.97x (0.95x to 1.01x)0.96x (0.90x to 1.00x)
Reverse complement0.833 s0.113 s0.026 s0.027 s7.42x (6.95x to 7.61x)31x (29x to 34x) (too short on ry typed)31x (29x to 33x) (too short on ry typed, AOT)
Reverse complement, whole file1.171 s0.116 s0.032 s0.033 s9.88x (9.57x to 10x)37x (35x to 38x) (too short on ry typed)35x (34x to 39x) (too short on ry typed, AOT)
Reverse complement, scalar lookup1.768 s0.292 s0.031 s0.032 s5.87x (5.70x to 6.22x)55x (54x to 58x) (too short on ry typed)54x (51x to 57x) (too short on ry typed, AOT)
Spectral norm1.637 s0.068 s0.031 s0.044 s21x (20x to 24x) (too short on ry, wide interval on ry)50x (46x to 55x) (too short on ry typed)36x (33x to 38x) (too short on ry typed, AOT)
Spectral norm, precomputed matrix1.355 s0.625 s0.732 s0.727 s2.14x (2.10x to 2.22x)1.83x (1.75x to 1.91x)1.78x (1.73x to 1.86x)
Spectral norm, matrix rebuilt per product2.120 s0.731 s0.936 s0.930 s3.05x (2.82x to 3.09x)2.32x (2.10x to 2.39x)2.28x (2.21x to 2.33x)
Spectral norm, element-wise matrix fill2.137 s0.840 s0.058 s0.060 s2.60x (2.41x to 2.69x)37x (34x to 38x) (too short on ry typed)37x (33x to 38x) (too short on ry typed, AOT, wide interval on ry typed, AOT)
Spectral norm, eigenvalues1.681 s1.462 s2.281 s2.326 s1.16x (1.11x to 1.19x)0.74x (0.72x to 0.75x)0.72x (0.70x to 0.75x)
Spectral norm, scalar loops1.571 s0.069 s0.024 s0.027 s23x (20x to 26x) (too short on ry, wide interval on ry)63x (59x to 68x) (too short on ry typed)55x (52x to 65x) (too short on ry typed, AOT, wide interval on ry typed, AOT)
Many local assignments0.574 s0.055 s0.003 s0.003 s10x (9.95x to 11x) (too short on ry)189x (184x to 199x) (too short on ry typed)193x (155x to 210x) (too short on ry typed, AOT, wide interval on ry typed, AOT)
Convolution, scalar loops0.966 s0.134 s0.037 s0.038 s7.40x (6.57x to 7.58x) (wide interval on ry)25x (25x to 27x) (too short on ry typed)25x (24x to 26x) (too short on ry typed, AOT)
Convolution, [[ indexing1.044 s0.193 s0.030 s0.031 s5.24x (5.10x to 5.54x)34x (32x to 36x) (too short on ry typed)35x (33x to 35x) (too short on ry typed, AOT)
Convolution, vectorized1.593 s1.002 s0.289 s0.276 s1.60x (1.51x to 1.62x)5.55x (5.35x to 5.77x)5.75x (5.44x to 5.90x)
Empty for loop0.751 s0.183 s0.074 s0.075 s4.11x (4.02x to 4.23x)10x (10x to 10x) (too short on ry typed)10x (9.64x to 10x) (too short on ry typed, AOT)
Counting while loop0.507 s0.061 s0.017 s0.017 s8.20x (7.82x to 8.56x) (too short on ry)30x (25x to 32x) (too short on ry typed, wide interval on ry typed)30x (28x to 31x) (too short on ry typed, AOT)
lapply with a closure0.919 s0.264 s0.015 s0.015 s3.47x (3.35x to 3.53x)61x (57x to 63x) (too short on ry typed)60x (55x to 61x) (too short on ry typed, AOT)
lapply with an extra argument1.165 s0.345 s0.016 s0.016 s3.33x (3.28x to 3.44x)75x (71x to 79x) (too short on ry typed)74x (71x to 77x) (too short on ry typed, AOT)
Growing a list, for loop1.192 s0.231 s0.011 s0.012 s5.17x (5.06x to 5.28x)102x (93x to 104x) (too short on ry typed)99x (93x to 106x) (too short on ry typed, AOT)
Growing a list, while loop1.244 s0.243 s0.012 s0.013 s5.09x (4.85x to 5.15x)98x (94x to 104x) (too short on ry typed)98x (88x to 104x) (too short on ry typed, AOT, wide interval on ry typed, AOT)
Matrix element assignment0.658 s0.199 s0.054 s0.054 s3.26x (3.20x to 3.44x)12x (12x to 13x) (too short on ry typed)12x (12x to 13x) (too short on ry typed, AOT)
Scalar assignment, for loop1.696 s0.138 s0.039 s0.038 s12x (12x to 13x)42x (35x to 44x) (too short on ry typed, wide interval on ry typed)44x (42x to 46x) (too short on ry typed, AOT)
Scalar assignment, while loop1.534 s0.115 s0.075 s0.075 s14x (13x to 14x)21x (20x to 22x) (too short on ry typed)20x (20x to 22x) (too short on ry typed, AOT)
Superassignment loop1.525 s0.318 s0.005 s0.006 s4.73x (4.55x to 4.87x)297x (245x to 305x) (too short on ry typed, wide interval on ry typed)273x (248x to 276x) (too short on ry typed, AOT)
Vector element assignment, for loop1.139 s0.322 s0.143 s0.146 s3.51x (3.31x to 3.71x)7.89x (7.61x to 8.07x)7.70x (7.35x to 8.02x)
Vector element assignment, while loop1.183 s0.164 s0.142 s0.142 s6.90x (6.23x to 7.22x)8.21x (7.84x to 8.49x)7.88x (7.68x to 8.27x)

Each time is the median over the rounds. Each speed-up is the median of the per-round ratios, with its 95% interval in parentheses. ry typed, AOT compiles each typed function before the program starts. Its time is the program's clock plus that compile, so it counts the same work as the other columns.

Measured on 2026-11-16 on the ry 0.5.0 release archive (sha256 575ad8ad890b) with GNU R 4.6.2 on Intel(R) Xeon(R) Processor @ 2.10GHz, 4 cores, Linux 6.18.44-fc-v470; 10 paired rounds.

Are We Fast Yet. R translations of programs from the Are We Fast Yet suite, which compares language implementations on the same small object and loop workloads. Bounce comes in three variants that differ in how a ball's fields are indexed. Source: github.com/PRL-PRG/r-compile-server/tree/b2b13a3150bddcc2d6dae8c37ecbbf1007170940/client/rsh/inst/benchmarks/areWeFast, MIT.

Bouncing balls, named fields bounce 2.24x

Moves 3,000 balls for 50 steps with lapply, reading and writing each ball's fields by name (ball['xVel']) Source

source('random.r')
execute <- function (ballCount = 3000L) {
bounces <- 0
balls = vector("list", length = ballCount)
resetSeed()
for (i in 1:ballCount) {
balls[[i]] = c(nextRandom() %% 500, nextRandom() %% 500,
(nextRandom() %% 300) - 150, (nextRandom() %% 300) - 150)
names(balls[[i]]) = c("x", "y", "xVel", "yVel")
}
ball <- function(ball) {
results <- bounce(ball)
if (results[[2]]) bounces <<- bounces + 1
return (results[[1]])
}
for (i in 1:50) balls <- lapply(balls, ball)
return (bounces)
}
bounce <- function(ball) {
xLimit <- 500
yLimit <- 500
bounced <- FALSE
ball['x'] <- ball['x'] + ball['xVel'];
ball['y'] <- ball['y'] + ball['yVel'];
if (ball['x'] > xLimit) {
ball['x'] <- xLimit
ball['xVel'] <- 0 - abs(ball['xVel'])
bounced <- TRUE
}
if (ball['x'] < 0) {
ball['x'] <- 0
ball['xVel'] <- abs(ball['xVel'])
bounced <- TRUE
}
if (ball['y'] > yLimit) {
ball['y'] <- yLimit
ball['yVel'] <- 0 - abs(ball['yVel'])
bounced <- TRUE
}
if (ball['y'] < 0) {
ball['y'] <- 0
ball['yVel'] <- abs(ball['yVel'])
bounced <- TRUE
}
return (list(ball, bounced))
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(3000L))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("bounce %s %.4f\n", checksum, seconds))
})
Bouncing balls, positional fields bounce_nonames 2.58x

Moves 10,000 balls for 50 steps with lapply, indexing each ball's fields by position and counting bounces through <<- Source

execute <- function (unused) {
seed <- NaN
resetSeed <- function() seed <<- 74755
nextRandom <- function() {
seed <<- bitwAnd((seed * 1309) + 13849, 65535)
return (seed)
}
ballCount <- 10000
bounces <- 0
balls = vector("list", length = ballCount)
resetSeed()
for (i in 1:ballCount) {
balls[[i]] = c(nextRandom() %% 500, nextRandom() %% 500,
(nextRandom() %% 300) - 150, (nextRandom() %% 300) - 150)
}
ball <- function(ball) {
results <- bounce(ball)
if (results[[2]]) bounces <<- bounces + 1
return (results[[1]])
}
for (i in 1:50) balls <- lapply(balls, ball)
return (bounces)
}
bounce <- function(ball) {
xLimit <- 500
yLimit <- 500
bounced <- FALSE
ball[1] <- ball[1] + ball[3];
ball[2] <- ball[2] + ball[4];
if (ball[1] > xLimit) {
ball[1] <- xLimit
ball[3] <- 0 - abs(ball[3])
bounced <- TRUE
}
if (ball[1] < 0) {
ball[1] <- 0
ball[3] <- abs(ball[3])
bounced <- TRUE
}
if (ball[2] > yLimit) {
ball[2] <- yLimit
ball[4] <- 0 - abs(ball[4])
bounced <- TRUE
}
if (ball[2] < 0) {
ball[2] <- 0
ball[4] <- abs(ball[4])
bounced <- TRUE
}
return (list(ball, bounced))
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(NULL))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("bounce_nonames %s %.4f\n", checksum, seconds))
})
Bouncing balls, positional fields, simple setup bounce_nonames_simple 2.61x

Same as the positional variant, but each ball's four random numbers are drawn into locals before c() builds it Source

execute <- function (unused) {
seed <- NaN
resetSeed <- function() seed <<- 74755
nextRandom <- function() {
seed <<- bitwAnd((seed * 1309) + 13849, 65535)
return (seed)
}
ballCount <- 10000
bounces <- 0
balls = vector("list", length = ballCount)
resetSeed()
for (i in 1:ballCount) {
random1 <- nextRandom()
random2 <- nextRandom()
random3 <- nextRandom()
random4 <- nextRandom()
balls[[i]] = c(random1 %% 500, random2 %% 500,
(random3 %% 300) - 150, (random4 %% 300) - 150)
}
ball <- function(ball) {
results <- bounce(ball)
if (results[[2]]) bounces <<- bounces + 1
return (results[[1]])
}
for (i in 1:50) balls <- lapply(balls, ball)
return (bounces)
}
bounce <- function(ball) {
xLimit <- 500
yLimit <- 500
bounced <- FALSE
ball[1] <- ball[1] + ball[3];
ball[2] <- ball[2] + ball[4];
if (ball[1] > xLimit) {
ball[1] <- xLimit
ball[3] <- 0 - abs(ball[3])
bounced <- TRUE
}
if (ball[1] < 0) {
ball[1] <- 0
ball[3] <- abs(ball[3])
bounced <- TRUE
}
if (ball[2] > yLimit) {
ball[2] <- yLimit
ball[4] <- 0 - abs(ball[4])
bounced <- TRUE
}
if (ball[2] < 0) {
ball[2] <- 0
ball[4] <- abs(ball[4])
bounced <- TRUE
}
return (list(ball, bounced))
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(NULL))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("bounce_nonames_simple %s %.4f\n", checksum, seconds))
})
Mandelbrot set, scalar loops mandelbrot 3.57x

Computes a 450 x 450 Mandelbrot bitmap checksum with nested scalar while loops, double arithmetic and bitwShiftL/bitwXor Source

lim <- 2
iter <- 50
execute <- function(size=450L) {
sum = 0
byteAcc = 0
bitNum = 0
y = 0
while (y < size) {
ci = (2.0 * y / size) - 1.0
x = 0
while (x < size) {
zr = 0.0
zrzr = 0.0
zi = 0.0
zizi = 0.0
cr = (2.0 * x / size) - 1.5
z = 0
notDone = TRUE
escape = 0
while (notDone && (z < 50)) {
zr = zrzr - zizi + cr
zi = 2.0 * zr * zi + ci
# preserve recalculation
zrzr = zr * zr
zizi = zi * zi
if ((zrzr + zizi) > 4.0) {
notDone = FALSE
escape = 1
}
z = z + 1
}
byteAcc = bitwShiftL(byteAcc, 1) + escape
bitNum = bitNum + 1
# Code is very similar for these cases, but using separate blocks
# ensures we skip the shifting when it's unnecessary, which is most cases.
if (bitNum == 8) {
sum = bitwXor(sum, byteAcc)
byteAcc = 0
bitNum = 0
} else if (x == (size - 1)) {
byteAcc = bitwShiftL(byteAcc, 8 - bitNum)
sum = bitwXor(sum, byteAcc)
byteAcc = 0
bitNum = 0
}
x = x + 1
}
y = y + 1
}
return (sum);
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(450L))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("mandelbrot %s %.4f\n", checksum, seconds))
})
Tree allocation storage 3.15x

Recursively builds a depth-10 tree of 4-element lists with random leaves, stressing allocation, recursion and <<- Source

source('random.r')
count <- 0
execute <- function(size) {
buildTreeDepth <- function(depth, random) {
count <<- count + 1
if (depth == 1) {
return (c(nextRandom() %% 10 + 1))
} else {
array <- vector("list", length = 4)
for (i in 1:4) {
array[[i]] <- buildTreeDepth(depth - 1, random)
}
return (array)
}
}
count <<- 0
resetSeed()
buildTreeDepth(10, nextRandom())
return (count)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(NULL))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("storage %s %.4f\n", checksum, seconds))
})

Benchmarks Game. R ports of the Computer Language Benchmarks Game programs, each in several variants that range from idiomatic vectorized R to naive scalar loops. The input-reading programs read a FASTA file that the driver generates with fasta.R before timing starts. Source: github.com/PRL-PRG/r-compile-server/tree/b2b13a3150bddcc2d6dae8c37ecbbf1007170940/client/rsh/inst/benchmarks/shootout, MIT.

Binary trees binarytrees 1.87x

Allocates and walks recursive list trees up to depth 12, with integer arithmetic and sapply over the iterations Source

execute <- function(n=9) {
tree <- function(item, depth) {
if (depth == 0L)
return(c(item, NA, NA))
# it is ridiculous that this doesn't help
next_depth <- depth - 1L
right_item <- 2L * item
left_item <- right_item - 1L
return(list(item,
tree(left_item, next_depth),
tree(right_item, next_depth)))
}
check <- function(tree)
if(is.na(tree[[2]][[1]])) tree[[1]] else tree[[1]] + check(tree[[2]]) - check(tree[[3]])
binarytrees <- function(args) {
n = if (length(args)) as.integer(args[[1]]) else 13L
min_depth <- 4L
max_depth <- max(min_depth + 2L, n)
stretch_depth <- max_depth + 1L
cat(sep="", "stretch tree of depth ", stretch_depth, "\t check: ",
check(tree(0L, stretch_depth)), "\n")
long_lived_tree <- tree(0L, max_depth)
for (depth in seq(min_depth, max_depth, 2L)) {
iterations <- as.integer(2^(max_depth - depth + min_depth))
check_sum <- sum(sapply(
1:iterations,
function(i) check(tree(i, depth)) + check(tree(-i, depth))))
cat(sep="", iterations * 2L, "\t trees of depth ", depth, "\t check: ",
check_sum, "\n")
}
cat(sep="", "long lived tree of depth ", max_depth, "\t check: ",
check(long_lived_tree), "\n")
}
binarytrees(n)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(12))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("binarytrees %s %.4f\n", checksum, seconds))
})
Binary trees, inline arithmetic binarytrees_2 1.70x

Binary trees with the child items computed inline in the recursive call and double rather than integer depths Source

execute <- function(n=9) {
tree <- function(item, depth) {
if (depth == 0L)
return(c(item, NA, NA))
return(list(item,
tree(2L * item - 1L, depth - 1L),
tree(2L * item, depth - 1L)))
}
check <- function(tree)
if(is.na(tree[[2]][[1]])) tree[[1]] else tree[[1]] + check(tree[[2]]) - check(tree[[3]])
binarytrees_2 <- function(args) {
n = if (length(args)) as.integer(args[[1]]) else 10L
min_depth <- 4
max_depth <- max(min_depth + 2, n)
stretch_depth <- max_depth + 1
cat(sep="", "stretch tree of depth ", stretch_depth, "\t check: ",
check(tree(0, stretch_depth)), "\n")
long_lived_tree <- tree(0, max_depth)
for (depth in seq(min_depth, max_depth, 2)) {
iterations <- as.integer(2^(max_depth - depth + min_depth))
check_sum <- sum(sapply(
1:iterations,
function(i) check(tree(i, depth)) + check(tree(-i, depth))))
cat(sep="", iterations * 2L, "\t trees of depth ", depth, "\t check: ",
check_sum, "\n")
}
cat(sep="", "long lived tree of depth ", max_depth, "\t check: ",
check(long_lived_tree), "\n")
}
binarytrees_2(n)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(11))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("binarytrees_2 %s %.4f\n", checksum, seconds))
})
Binary trees, explicit loop binarytrees_naive 1.67x

Binary trees that sums each depth's checks in a for loop instead of sapply Source

execute <- function(n=9) {
tree <- function(item, depth) {
if (depth == 0L)
return(c(item, NA, NA))
return(list(item,
tree(2L * item - 1L, depth - 1L),
tree(2L * item, depth - 1L)))
}
check <- function(tree)
if(is.na(tree[[2]][[1]])) tree[[1]] else tree[[1]] + check(tree[[2]]) - check(tree[[3]])
binarytrees_naive <- function(args) {
n = if(length(args)) as.integer(args[[1]]) else 13L
min_depth <- 4
max_depth <- max(min_depth + 2, n)
stretch_depth <- max_depth + 1
cat(sep="", "stretch tree of depth ", stretch_depth, "\t check: ",
check(tree(0, stretch_depth)), "\n")
long_lived_tree <- tree(0, max_depth)
for (depth in seq(min_depth, max_depth, 2)) {
iterations <- as.integer(2^(max_depth - depth + min_depth))
chk_sum <- 0L
for (i in 1:iterations)
chk_sum <- chk_sum + check(tree(i, depth)) + check(tree(-i, depth))
cat(sep="", iterations * 2L, "\t trees of depth ", depth, "\t check ",
chk_sum, "\n")
}
cat(sep="", "long lived tree of depth ", max_depth, "\t check: ",
check(long_lived_tree), "\n")
}
binarytrees_naive(n)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(12))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("binarytrees_naive %s %.4f\n", checksum, seconds))
})
Fannkuch-redux fannkuchredux 1.66x

Counts pancake flips over all permutations of 9 elements, reversing prefixes with vectorized rev and slice assignment Source

execute <- function(n=9) {
fannkuch <- function(n) {
one_two = c(1, 2)
two_one = c(2, 1)
two_three = c(2, 3)
three_two = c(3, 2)
if (n > 3L)
rxrange = 3:(n - 1)
else
rxrange = integer(0)
max_flip_count <- 0L
perm_sign <- TRUE
checksum <- 0L
perm1 <- 1:n
count <- 0:(n - 1L)
while (TRUE) {
if (k <- perm1[[1L]]) {
perm <- perm1
flip_count <- 1L
while ((kk <- perm[[k]]) > 1L) {
k_range <- 1:k
perm[k_range] <- rev.default(perm[k_range])
flip_count <- flip_count + 1L
k <- kk
kk <- perm[[kk]]
}
max_flip_count <- max(max_flip_count, flip_count)
checksum <- checksum + if (perm_sign) flip_count else -flip_count
}
# Use incremental change to generate another permutation
if (perm_sign) {
perm1[one_two] <- perm1[two_one]
perm_sign <- FALSE
} else {
perm1[two_three] <- perm1[three_two]
perm_sign = TRUE
was_break <- FALSE
for (r in rxrange) {
if (count[[r]]) {
was_break <- TRUE
break
}
count[[r]] <- r - 1L
perm0 <- perm1[[1L]]
perm1[1:r] <- perm1[2:(r + 1L)]
perm1[[r + 1L]] <- perm0
}
if (!was_break) {
r <- n
if (!count[[r]]) {
cat(checksum, "\n", sep="")
return(max_flip_count)
}
}
count[[r]] <- count[[r]] - 1L
}
}
}
fannkuchredux <- function(args) {
n <- if (length(args)) as.integer(args[[1]]) else 12L
fannkuch(n)
}
fannkuchredux(n)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(9))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("fannkuchredux %s %.4f\n", checksum, seconds))
})
Fannkuch-redux, scalar swaps fannkuchredux_naive 4.37x

Fannkuch-redux that reverses prefixes and rotates the permutation one element at a time with [[ swaps Source

execute <- function(n=9) {
fannkuch <- function(n) {
if (n > 3L)
rxrange = 3:(n - 1)
else
rxrange = integer(0)
max_flip_count <- 0L
perm_sign <- TRUE
checksum <- 0L
perm1 <- 1:n
count <- 0:(n - 1L)
while (TRUE) {
if (k <- perm1[[1L]]) {
perm <- perm1
flip_count <- 1L
while ((kk <- perm[[k]]) > 1L) {
for (lo in 1:(k %/% 2L)) {
hi = k - lo + 1L
t <- perm[[lo]]; perm[[lo]] <- perm[[hi]]; perm[[hi]] <- t
}
flip_count <- flip_count + 1L
k <- kk
kk <- perm[[kk]]
}
max_flip_count <- max(max_flip_count, flip_count)
checksum <- checksum + if (perm_sign) flip_count else -flip_count
}
# Use incremental change to generate another permutation
if (perm_sign) {
t <- perm1[[1]]; perm1[[1]] <- perm1[[2]]; perm1[[2]] <- t
perm_sign = FALSE
} else {
t <- perm1[[2]]; perm1[[2]] <- perm1[[3]]; perm1[[3]] <- t
perm_sign = TRUE
was_break <- FALSE
for (r in rxrange) {
if (count[[r]]) {
was_break <- TRUE
break
}
count[[r]] <- r - 1L
perm0 <- perm1[[1L]]
for (i in 1:r)
perm1[[i]] <- perm1[[i + 1L]]
perm1[[r + 1L]] <- perm0
}
if (!was_break) {
r <- n
if (!count[[r]]) {
cat(checksum, "\n", sep="")
return(max_flip_count)
}
}
count[[r]] <- count[[r]] - 1L
}
}
}
fannkuchredux_naive <- function(args) {
n = if (length(args)) as.integer(args[[1]]) else 12L
fannkuch(n)
}
fannkuchredux_naive(n)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(9))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("fannkuchredux_naive %s %.4f\n", checksum, seconds))
})
Fasta fasta 3.43x

Generates DNA sequences with an integer LCG, picking each line's codes at once with outer() and colSums Source

execute <- function(n=60000) {
width <- 60L
myrandom_last <- 42L
myrandom <- function(m) {
myrandom_last <<- (myrandom_last * 3877L + 29573L) %% 139968L
return(m * myrandom_last / 139968)
}
alu <- paste(
"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG",
"GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA",
"CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT",
"ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA",
"GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG",
"AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC",
"AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA",
sep="", collapse="")
iub <- matrix(c(
c(0.27, 'a'),
c(0.12, 'c'),
c(0.12, 'g'),
c(0.27, 't'),
c(0.02, 'B'),
c(0.02, 'D'),
c(0.02, 'H'),
c(0.02, 'K'),
c(0.02, 'M'),
c(0.02, 'N'),
c(0.02, 'R'),
c(0.02, 'S'),
c(0.02, 'V'),
c(0.02, 'W'),
c(0.02, 'Y')
), 2)
homosapiens <- matrix(c(
c(0.3029549426680, 'a'),
c(0.1979883004921, 'c'),
c(0.1975473066391, 'g'),
c(0.3015094502008, 't')
), 2)
repeat_fasta <- function(s, count) {
chars <- strsplit(s, split="")[[1]]
len <- nchar(s)
s2 <- c(chars, chars[1:width])
pos <- 1L
while (count) {
line <- min(width, count)
next_pos <- pos + line
cat(s2[pos:(next_pos - 1)], "\n", sep="")
pos <- next_pos
if (pos > len) pos <- pos - len
count <- count - line
}
}
random_fasta <- function(genelist, count) {
psum <- cumsum(genelist[1,])
while (count) {
line <- min(width, count)
rs <- double(line)
for (i in 1:line)
rs[[i]] <- myrandom(1)
cat(genelist[2, colSums(outer(psum, rs, "<")) + 1], "\n", sep='')
count <- count - line
}
}
fasta <- function(args) {
n = if (length(args)) as.integer(args[[1]]) else 1000L
cat(">ONE Homo sapiens alu\n")
repeat_fasta(alu, 2 * n)
cat(">TWO IUB ambiguity codes\n")
random_fasta(iub, 3L * n)
cat(">THREE Homo sapiens frequency\n")
random_fasta(homosapiens, 5L * n)
}
fasta(n)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(60000))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("fasta %s %.4f\n", checksum, seconds))
})
Fasta, vectorized linear search fasta_2 3.86x

Fasta that picks each line's codes by a vectorized linear search, advancing indices with which() until all match Source

execute <- function(n=60000) {
width <- 60L
myrandom_last <- 42L
myrandom <- function(m) {
myrandom_last <<- (myrandom_last * 3877L + 29573L) %% 139968L
return(m * myrandom_last / 139968)
}
alu <- paste(
"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG",
"GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA",
"CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT",
"ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA",
"GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG",
"AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC",
"AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA",
sep="", collapse="")
iub <- matrix(c(
c(0.27, 'a'),
c(0.12, 'c'),
c(0.12, 'g'),
c(0.27, 't'),
c(0.02, 'B'),
c(0.02, 'D'),
c(0.02, 'H'),
c(0.02, 'K'),
c(0.02, 'M'),
c(0.02, 'N'),
c(0.02, 'R'),
c(0.02, 'S'),
c(0.02, 'V'),
c(0.02, 'W'),
c(0.02, 'Y')
), 2)
homosapiens <- matrix(c(
c(0.3029549426680, 'a'),
c(0.1979883004921, 'c'),
c(0.1975473066391, 'g'),
c(0.3015094502008, 't')
), 2)
repeat_fasta <- function(s, count) {
chars <- strsplit(s, split="")[[1]]
len <- nchar(s)
s2 <- c(chars, chars[1:width])
pos <- 1L
while (count) {
line <- min(width, count)
next_pos <- pos + line
cat(s2[pos:(next_pos - 1)], "\n", sep="")
pos <- next_pos
if (pos > len) pos <- pos - len
count <- count - line
}
}
random_fasta <- function(genelist, count) {
psum = cumsum(genelist[1,])
n = length(psum)
while (count) {
line = min(width, count)
rs <- double(line)
for (i in 1:line)
rs[[i]] <- myrandom(1)
# Linear search (vectorized)
inds <- 1:line
lo <- rep.int(1L, line)
while (length(inds <- which(psum[lo] < rs)))
lo[inds] <- lo[inds] + 1L
cat(genelist[2, lo], "\n", sep='')
count <- count - line
}
}
fasta_2 <- function(args) {
n = if (length(args)) as.integer(args[[1]]) else 1000L
cat(">ONE Homo sapiens alu\n")
repeat_fasta(alu, 2 * n)
cat(">TWO IUB ambiguity codes\n")
random_fasta(iub, 3L * n)
cat(">THREE Homo sapiens frequency\n")
random_fasta(homosapiens, 5L * n)
}
fasta_2(n)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(60000))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("fasta_2 %s %.4f\n", checksum, seconds))
})
Fasta, vectorized binary search fasta_3 3.93x

Fasta that picks each line's codes by a vectorized binary search over the cumulative probabilities Source

execute <- function(n=60000) {
width <- 60L
myrandom_last <- 42L
myrandom <- function(m) {
myrandom_last <<- (myrandom_last * 3877L + 29573L) %% 139968L
return(m * myrandom_last / 139968)
}
alu <- paste(
"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG",
"GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA",
"CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT",
"ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA",
"GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG",
"AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC",
"AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA",
sep="", collapse="")
iub <- matrix(c(
c(0.27, 'a'),
c(0.12, 'c'),
c(0.12, 'g'),
c(0.27, 't'),
c(0.02, 'B'),
c(0.02, 'D'),
c(0.02, 'H'),
c(0.02, 'K'),
c(0.02, 'M'),
c(0.02, 'N'),
c(0.02, 'R'),
c(0.02, 'S'),
c(0.02, 'V'),
c(0.02, 'W'),
c(0.02, 'Y')
), 2)
homosapiens <- matrix(c(
c(0.3029549426680, 'a'),
c(0.1979883004921, 'c'),
c(0.1975473066391, 'g'),
c(0.3015094502008, 't')
), 2)
repeat_fasta <- function(s, count) {
chars <- strsplit(s, split="")[[1]]
len <- nchar(s)
s2 <- c(chars, chars[1:width])
pos <- 1L
while (count) {
line <- min(width, count)
next_pos <- pos + line
cat(s2[pos:(next_pos - 1)], "\n", sep="")
pos <- next_pos
if (pos > len) pos <- pos - len
count <- count - line
}
}
random_fasta <- function(genelist, count) {
psum = cumsum(genelist[1,])
n = length(psum)
while (count) {
line = min(width, count)
rs <- double(line)
for (i in 1:line)
rs[[i]] <- myrandom(1)
# Binary search (vectorized)
inds <- 1:line
lo <- rep.int(1L, line); hi <- rep.int(n, line)
mid <- integer(line)
repeat {
mid[inds] <- (lo[inds] + hi[inds] - 1L) %/% 2
ge_inds = which(psum[mid] >= rs)
hi[ge_inds] <- mid[ge_inds]
lt_inds = which(psum[mid] < rs)
lo[lt_inds] <- mid[lt_inds] + 1L
inds <- which(lo < hi)
if (!length(inds))
break
}
cat(genelist[2, hi], "\n", sep='')
count <- count - line
}
}
fasta_3 <- function(args) {
n = if (length(args)) as.integer(args[[1]]) else 1000L
cat(">ONE Homo sapiens alu\n")
repeat_fasta(alu, 2 * n)
cat(">TWO IUB ambiguity codes\n")
random_fasta(iub, 3L * n)
cat(">THREE Homo sapiens frequency\n")
random_fasta(homosapiens, 5L * n)
}
fasta_3(n)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(60000))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("fasta_3 %s %.4f\n", checksum, seconds))
})
Fasta, scalar linear search fasta_naive 4.53x

Fasta that picks each character with a scalar linear search loop and joins every line with paste Source

execute <- function(n=80000) {
width <- 60L
myrandom_last <- 42L
myrandom <- function(m) {
myrandom_last <<- (myrandom_last * 3877L + 29573L) %% 139968L
return(m * myrandom_last / 139968)
}
alu <- paste(
"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG",
"GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA",
"CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT",
"ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA",
"GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG",
"AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC",
"AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA",
sep="", collapse="")
iub <- matrix(c(
c(0.27, 'a'),
c(0.12, 'c'),
c(0.12, 'g'),
c(0.27, 't'),
c(0.02, 'B'),
c(0.02, 'D'),
c(0.02, 'H'),
c(0.02, 'K'),
c(0.02, 'M'),
c(0.02, 'N'),
c(0.02, 'R'),
c(0.02, 'S'),
c(0.02, 'V'),
c(0.02, 'W'),
c(0.02, 'Y')
), 2)
homosapiens <- matrix(c(
c(0.3029549426680, 'a'),
c(0.1979883004921, 'c'),
c(0.1975473066391, 'g'),
c(0.3015094502008, 't')
), 2)
repeat_fasta <- function(s, count) {
chars = strsplit(s, split="")[[1]]
len = nchar(s)
s2 <- character(len + width)
for (i in 1:len)
s2[[i]] <- chars[[i]]
for (i in 1:width)
s2[[len + i]] <- chars[[i]]
pos <- 1L
while (count) {
line = min(width, count)
next_pos <- pos + line
cat(paste(s2[pos:(next_pos - 1)], collapse="", sep=""), "\n", sep="")
pos <- next_pos
if (pos > len) pos <- pos - len
count <- count - line
}
}
random_fasta <- function(genelist, count) {
psum = cumsum(genelist[1,])
n = length(psum)
while (count) {
line = min(width, count)
seq <- character(line)
for (i in 1:line) {
r <- myrandom(1)
lo <- 1L
while (psum[[lo]] < r)
lo <- lo + 1L
seq[[i]] <- genelist[[2, lo]]
}
cat(paste(seq, collapse="", sep=""), "\n", sep='')
count <- count - line
}
}
fasta_naive <- function(args) {
n = if (length(args)) as.integer(args[[1]]) else 1000L
cat(">ONE Homo sapiens alu\n")
repeat_fasta(alu, 2 * n)
cat(">TWO IUB ambiguity codes\n")
random_fasta(iub, 3L * n)
cat(">THREE Homo sapiens frequency\n")
random_fasta(homosapiens, 5L * n)
}
fasta_naive(n)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(80000))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("fasta_naive %s %.4f\n", checksum, seconds))
})
Fasta, scalar binary search fasta_naive_2 3.40x

Fasta that picks each character with a scalar binary search loop and joins every line with paste Source

execute <- function(n=80000) {
width <- 60L
myrandom_last <- 42L
myrandom <- function(m) {
myrandom_last <<- (myrandom_last * 3877L + 29573L) %% 139968L
return(m * myrandom_last / 139968)
}
alu <- paste(
"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG",
"GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA",
"CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT",
"ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA",
"GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG",
"AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC",
"AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA",
sep="", collapse="")
iub <- matrix(c(
c(0.27, 'a'),
c(0.12, 'c'),
c(0.12, 'g'),
c(0.27, 't'),
c(0.02, 'B'),
c(0.02, 'D'),
c(0.02, 'H'),
c(0.02, 'K'),
c(0.02, 'M'),
c(0.02, 'N'),
c(0.02, 'R'),
c(0.02, 'S'),
c(0.02, 'V'),
c(0.02, 'W'),
c(0.02, 'Y')
), 2)
homosapiens <- matrix(c(
c(0.3029549426680, 'a'),
c(0.1979883004921, 'c'),
c(0.1975473066391, 'g'),
c(0.3015094502008, 't')
), 2)
repeat_fasta <- function(s, count) {
chars = strsplit(s, split="")[[1]]
len = nchar(s)
s2 <- character(len + width)
for (i in 1:len)
s2[[i]] <- chars[[i]]
for (i in 1:width)
s2[[len + i]] <- chars[[i]]
pos <- 1L
while (count) {
line = min(width, count)
next_pos <- pos + line
cat(paste(s2[pos:(next_pos - 1)], collapse="", sep=""), "\n", sep="")
pos <- next_pos
if (pos > len) pos <- pos - len
count <- count - line
}
}
random_fasta <- function(genelist, count) {
psum = cumsum(genelist[1,])
n = length(psum)
while (count) {
line = min(width, count)
seq <- character(line)
for (i in 1:line) {
r <- myrandom(1)
# Binary search
lo <- 1L; hi <- n
while (lo < hi) {
mid <- (lo + hi - 1L) %/% 2
if (psum[[mid]] >= r) hi <- mid
else lo <- mid + 1L
}
seq[[i]] <- genelist[[2, hi]]
}
cat(paste(seq, collapse="", sep=""), "\n", sep='')
count <- count - line
}
}
fasta_naive_2 <- function(args) {
n = if (length(args)) as.integer(args[[1]]) else 1000L
cat(">ONE Homo sapiens alu\n")
repeat_fasta(alu, 2 * n)
cat(">TWO IUB ambiguity codes\n")
random_fasta(iub, 3L * n)
cat(">THREE Homo sapiens frequency\n")
random_fasta(homosapiens, 5L * n)
}
fasta_naive_2(n)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(80000))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("fasta_naive_2 %s %.4f\n", checksum, seconds))
})
Fasta-redux fastaredux 4.72x

Fasta with a 4,096-entry lookup table that maps a random number to a code, corrected for a whole line at a time Source

execute <- function(n=80000) {
width = 60L
lookup_size = 4096L
lookup_scale = as.double(lookup_size - 1L)
alu = paste(
"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG",
"GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA",
"CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT",
"ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA",
"GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG",
"AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC",
"AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA",
sep="", collapse="")
iub = matrix(c(
c(0.27, 'a'),
c(0.12, 'c'),
c(0.12, 'g'),
c(0.27, 't'),
c(0.02, 'B'),
c(0.02, 'D'),
c(0.02, 'H'),
c(0.02, 'K'),
c(0.02, 'M'),
c(0.02, 'N'),
c(0.02, 'R'),
c(0.02, 'S'),
c(0.02, 'V'),
c(0.02, 'W'),
c(0.02, 'Y')
), 2)
homosapiens = matrix(c(
c(0.3029549426680, 'a'),
c(0.1979883004921, 'c'),
c(0.1975473066391, 'g'),
c(0.3015094502008, 't')
), 2)
random <- 42L
random_next_lookup <- function() {
random <<- (random * 3877L + 29573L) %% 139968L
return(random * (lookup_scale / 139968)) # TODO
}
repeat_fasta <- function(s, count) {
chars = strsplit(s, split="")[[1]]
len = nchar(s)
s2 = c(chars, chars[1:width])
pos <- 1L
while (count) {
line = min(width, count)
next_pos <- pos + line
cat(s2[pos:(next_pos - 1)], "\n", sep="")
pos <- next_pos
if (pos > len) pos <- pos - len
count <- count - line
}
}
random_fasta <- function(genelist, count) {
n = ncol(genelist)
lookup <- integer(lookup_size)
cprob_lookup <- cumsum(genelist[1, ]) * lookup_scale
cprob_lookup[[n]] <- lookup_size - 1
j <- 1L
for (i in 1:lookup_size) {
while (cprob_lookup[[j]] + 1L < i)
j <- j + 1L
lookup[[i]] <- j
}
while (count) {
line <- min(width, count)
rs <- double(line)
for (i in 1:line)
rs[[i]] <- random_next_lookup()
inds <- lookup[rs + 1L]
missed <- which(cprob_lookup[inds] < rs)
if (length(missed))
repeat {
inds[missed] <- inds[missed] + 1L
missed <- which(cprob_lookup[inds] < rs)
if (!length(missed))
break
}
cat(paste(genelist[2, inds], collapse="", sep=""), "\n", sep="")
count <- count - line
}
}
fastaredux <- function(args) {
n = if (length(args)) as.integer(args[[1]]) else 1000L
cat(">ONE Homo sapiens alu\n")
repeat_fasta(alu, 2 * n)
cat(">TWO IUB ambiguity codes\n")
random_fasta(iub, 3L * n)
cat(">THREE Homo sapiens frequency\n")
random_fasta(homosapiens, 5L * n)
}
fastaredux(n)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(80000))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("fastaredux %s %.4f\n", checksum, seconds))
})
Fasta-redux, scalar lookup fastaredux_naive 4.07x

Fasta-redux that does the table lookup and its correction one character at a time in a scalar loop Source

execute <- function(n=80000) {
width = 60L
lookup_size = 4096L
lookup_scale = as.double(lookup_size - 1L)
alu = paste(
"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG",
"GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA",
"CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT",
"ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA",
"GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG",
"AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC",
"AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA",
sep="", collapse="")
iub = matrix(c(
c(0.27, 'a'),
c(0.12, 'c'),
c(0.12, 'g'),
c(0.27, 't'),
c(0.02, 'B'),
c(0.02, 'D'),
c(0.02, 'H'),
c(0.02, 'K'),
c(0.02, 'M'),
c(0.02, 'N'),
c(0.02, 'R'),
c(0.02, 'S'),
c(0.02, 'V'),
c(0.02, 'W'),
c(0.02, 'Y')
), 2)
homosapiens = matrix(c(
c(0.3029549426680, 'a'),
c(0.1979883004921, 'c'),
c(0.1975473066391, 'g'),
c(0.3015094502008, 't')
), 2)
random <- 42L
random_next_lookup <- function() {
random <<- (random * 3877L + 29573L) %% 139968L
return(random * (lookup_scale / 139968)) # TODO
}
repeat_fasta <- function(s, count) {
chars = strsplit(s, split="")[[1]]
len = nchar(s)
s2 <- character(len + width)
for (i in 1:len)
s2[[i]] <- chars[[i]]
for (i in 1:width)
s2[[len + i]] <- chars[[i]]
pos <- 1L
while (count) {
line = min(width, count)
next_pos <- pos + line
cat(paste(s2[pos:(next_pos - 1)], collapse="", sep=""), "\n", sep="")
pos <- next_pos
if (pos > len) pos <- pos - len
count <- count - line
}
}
random_fasta <- function(genelist, count) {
n = ncol(genelist)
lookup <- integer(lookup_size)
cprob_lookup <- cumsum(genelist[1, ]) * lookup_scale
cprob_lookup[[n]] <- lookup_size - 1
j <- 1L
for (i in 1:lookup_size) {
while (cprob_lookup[[j]] + 1L < i)
j <- j + 1L
lookup[[i]] <- j
}
while (count) {
line <- min(width, count)
seq <- character(line)
for (i in 1:line) {
r <- random_next_lookup()
ind <- lookup[[r + 1L]]
if (cprob_lookup[[ind]] < r)
repeat {
ind <- ind + 1L
if (!(cprob_lookup[[ind]] < r))
break
}
seq[[i]] <- genelist[[2, ind]]
}
cat(paste(seq, collapse="", sep=""), "\n", sep="")
count <- count - line
}
}
fastaredux_naive <- function(args) {
n = if (length(args)) as.integer(args[[1]]) else 1000L
cat(">ONE Homo sapiens alu\n")
repeat_fasta(alu, 2 * n)
cat(">TWO IUB ambiguity codes\n")
random_fasta(iub, 3L * n)
cat(">THREE Homo sapiens frequency\n")
random_fasta(homosapiens, 5L * n)
}
fastaredux_naive(n)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(80000))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("fastaredux_naive %s %.4f\n", checksum, seconds))
})
K-nucleotide, hashed environment knucleotide 3.76x

Reads a FASTA file and counts k-mer frequencies in a hashed environment with exists, get and assign Source

execute <- function(n=2000) {
gen_freq <- function(seq, frame) {
frame <- frame - 1L
ns <- length(seq) - frame
h <- new.env(emptyenv(), hash=TRUE)
for (i in 1:ns) {
subseq_str = paste(seq[i:(i + frame)], collapse="", sep="")
if (exists(subseq_str, h, inherits=FALSE))
cnt <- get(subseq_str, h, inherits=FALSE)
else
cnt <- 0L
assign(subseq_str, cnt + 1L, h)
}
return(sapply(ls(h), function(k) get(k, h, inherits=FALSE)))
}
sort_seq <- function(seq, len) {
fs <- gen_freq(seq, len)
seqs <- names(fs)
inds <- order(-fs, seqs)
# cat(paste.(seqs[inds], 100 * fs[inds] / sum(fs), collapse="\n", digits=3),
cat(paste(seqs[inds], 100 * fs[inds] / sum(fs), collapse="\n"),
"\n")
}
find_seq <- function(seq, s) {
freqs <- gen_freq(seq, nchar(s))
if (s %in% names(freqs))
return(freqs[[s]])
return(0L)
}
knucleotide <- function(args) {
in_filename = args[[1]]
f <- file(in_filename, "r")
print(f)
while (length(line <- readLines(f, n=1, warn=FALSE))) {
first_char <- substr(line, 1L, 1L)
if (first_char == '>' || first_char == ';')
if (substr(line, 2L, 3L) == 'TH')
break
}
n <- 0L
cap <- 8L
str_buf <- character(cap)
while (length(line <- scan(f, what="", nmax=1, quiet=TRUE))) {
first_char <- substr(line, 1L, 1L)
if (first_char == '>' || first_char == ';')
break
n <- n + 1L
# ensure O(N) resizing (instead of O(N^2))
str_buf[[cap <- if (cap < n) 2L * cap else cap]] <- ""
str_buf[[n]] <- line
}
length(str_buf) <- n
close(f)
seq <- strsplit(paste(str_buf, collapse=""), split="")[[1]]
for (frame in 1:2)
sort_seq(seq, frame)
for (s in c("GGT", "GGTA", "GGTATT", "GGTATTTTAATT", "GGTATTTTAATTTATAGT"))
cat(find_seq(seq, tolower(s)), sep="\t", s, "\n")
}
paste. <- function (..., digits=16, sep=" ", collapse=NULL) {
args <- list(...)
if (length(args) == 0)
if (length(collapse) == 0) character(0)
else ""
else {
for(i in seq(along=args))
if(is.numeric(args[[i]]))
args[[i]] <- as.character(round(args[[i]], digits))
else args[[i]] <- as.character(args[[i]])
.Internal(paste(args, sep, collapse))
}
}
file <- paste("../fasta/fasta", n, ".txt", sep="")
knucleotide(file)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
# The program reads ../fasta/fasta<n>.txt, so write that input with this
# suite's fasta.R into a scratch directory first, outside the timed region.
generator <- new.env()
generator$.ry_bench_loading <- TRUE
sys.source("fasta.R", envir = generator)
scratch <- tempfile("shootout-")
dir.create(file.path(scratch, "fasta"), recursive = TRUE)
dir.create(file.path(scratch, "run"))
capture.output(generator$execute(4000L), file = file.path(scratch, "fasta", paste0("fasta", 4000L, ".txt")))
home <- setwd(file.path(scratch, "run"))
on.exit({ setwd(home); unlink(scratch, recursive = TRUE) })
started <- proc.time()[[3]]
output <- capture.output(value <- execute(4000L))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("knucleotide %s %.4f\n", checksum, seconds))
})
K-nucleotide, attribute counts knucleotide_brute 4.17x

K-nucleotide that keeps each k-mer's count as an attribute of an integer vector, read with attr and set with attr<- Source

execute <- function(n=2000) {
gen_freq <- function(seq, frame) {
frame <- frame - 1L
ns <- nchar(seq) - frame
freqs <- integer(0)
for (i in 1:ns) {
subseq = substr(seq, i, i + frame)
cnt <- attr(freqs, subseq)
if (is.null(cnt)) cnt <- 0L
attr(freqs, subseq) <- cnt + 1L
}
return(freqs)
}
gen_freq <- function(seq, frame) { # CTK: duplicate method
frame <- frame - 1L
ns <- nchar(seq) - frame
freqs <- integer(0)
for (i in 1:ns) {
subseq = substr(seq, i, i + frame)
cnt <- attr(freqs, subseq) # CTK: adding exact=TRUE would massively improve performance
if (is.null(cnt)) cnt <- 0L
attr(freqs, subseq) <- cnt + 1L
}
return(freqs)
}
sort_seq <- function(seq, len) {
cnt_map <- gen_freq(seq, len)
#print(cnt_map)
attrs <- attributes(cnt_map)
fs <- unlist(attrs, use.names=FALSE)
seqs <- toupper(paste(names(attrs)))
inds <- order(-fs, seqs)
#cat(paste(seqs[inds], fs[inds], collapse="\n"), "\n")
#cat(paste.(seqs[inds], 100 * fs[inds] / sum(fs), collapse="\n", digits=3),
cat(paste(seqs[inds], 100 * fs[inds] / sum(fs), collapse="\n"),
"\n")
}
find_seq <- function(seq, s) {
cnt_map <- gen_freq(seq, nchar(s))
if (!is.null(cnt <- attr(cnt_map, s)))
return(cnt)
return(0L)
}
knucleotide_brute <- function(args) {
in_filename = args[[1]]
f <- file(in_filename, "r")
while (length(line <- readLines(f, n=1, warn=FALSE))) {
first_char <- substr(line, 1L, 1L)
if (first_char == '>' || first_char == ';')
if (substr(line, 2L, 3L) == 'TH')
break
}
n <- 0L
cap <- 8L
str_buf <- character(cap)
while (length(line <- scan(f, what="", nmax=1, quiet=TRUE))) {
first_char <- substr(line, 1L, 1L)
if (first_char == '>' || first_char == ';')
break
n <- n + 1L
# ensure O(N) resizing (instead of O(N^2))
str_buf[[cap <- if (cap < n) 2L * cap else cap]] <- ""
str_buf[[n]] <- line
}
length(str_buf) <- n
close(f)
seq <- paste(str_buf, collapse="")
for (frame in 1:2)
sort_seq(seq, frame)
for (s in c("GGT", "GGTA", "GGTATT", "GGTATTTTAATT", "GGTATTTTAATTTATAGT"))
cat(find_seq(seq, tolower(s)), sep="\t", s, "\n")
}
paste. <- function (..., digits=16, sep=" ", collapse=NULL) {
args <- list(...)
if (length(args) == 0)
if (length(collapse) == 0) character(0)
else ""
else {
for(i in seq(along=args))
if(is.numeric(args[[i]]))
args[[i]] <- as.character(round(args[[i]], digits))
else args[[i]] <- as.character(args[[i]])
.Internal(paste(args, sep, collapse))
}
}
file <- paste("../fasta/fasta", n, ".txt", sep="")
knucleotide_brute(file)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
# The program reads ../fasta/fasta<n>.txt, so write that input with this
# suite's fasta.R into a scratch directory first, outside the timed region.
generator <- new.env()
generator$.ry_bench_loading <- TRUE
sys.source("fasta.R", envir = generator)
scratch <- tempfile("shootout-")
dir.create(file.path(scratch, "fasta"), recursive = TRUE)
dir.create(file.path(scratch, "run"))
capture.output(generator$execute(1000L), file = file.path(scratch, "fasta", paste0("fasta", 1000L, ".txt")))
home <- setwd(file.path(scratch, "run"))
on.exit({ setwd(home); unlink(scratch, recursive = TRUE) })
started <- proc.time()[[3]]
output <- capture.output(value <- execute(1000L))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("knucleotide_brute %s %.4f\n", checksum, seconds))
})
K-nucleotide, attribute counts, growing vector knucleotide_brute_2 3.00x

Attribute-count k-nucleotide that also grows the underlying vector by doubling each time a new k-mer appears Source

execute <- function(n=3000) {
gen_freq <- function(seq, frame) {
frame <- frame - 1L
ns <- nchar(seq) - frame
n <- 0L
cap <- 16L
freqs <- integer(cap)
for (i in 1:ns) {
subseq = substr(seq, i, i + frame)
cnt <- attr(freqs, subseq)
if (is.null(cnt)) {
cnt <- 0L
# ensure O(N) resizing (instead of O(N^2))
n <- n + 1L
freqs[[cap <- if (cap < n) 2L * cap else cap]] <- 0L
}
attr(freqs, subseq) <- cnt + 1L
}
return(freqs)
}
sort_seq <- function(seq, len) {
cnt_map <- gen_freq(seq, len)
#print(cnt_map)
attrs <- attributes(cnt_map)
fs <- unlist(attrs, use.names=FALSE)
seqs <- toupper(paste(names(attrs)))
inds <- order(-fs, seqs)
#cat(paste(seqs[inds], fs[inds], collapse="\n"), "\n")
#cat(paste.(seqs[inds], 100 * fs[inds] / sum(fs), collapse="\n", digits=3),
cat(paste(seqs[inds], 100 * fs[inds] / sum(fs), collapse="\n"),
"\n")
}
find_seq <- function(seq, s) {
cnt_map <- gen_freq(seq, nchar(s))
if (!is.null(cnt <- attr(cnt_map, s)))
return(cnt)
return(0L)
}
knucleotide_brute_2 <- function(args) {
in_filename = args[[1]]
f <- file(in_filename, "r")
while (length(line <- readLines(f, n=1, warn=FALSE))) {
first_char <- substr(line, 1L, 1L)
if (first_char == '>' || first_char == ';')
if (substr(line, 2L, 3L) == 'TH')
break
}
n <- 0L
cap <- 8L
str_buf <- character(cap)
while (length(line <- scan(f, what="", nmax=1, quiet=TRUE))) {
first_char <- substr(line, 1L, 1L)
if (first_char == '>' || first_char == ';')
break
n <- n + 1L
# ensure O(N) resizing (instead of O(N^2))
str_buf[[cap <- if (cap < n) 2L * cap else cap]] <- ""
str_buf[[n]] <- line
}
length(str_buf) <- n
close(f)
seq <- paste(str_buf, collapse="")
for (frame in 1:2)
sort_seq(seq, frame)
for (s in c("GGT", "GGTA", "GGTATT", "GGTATTTTAATT", "GGTATTTTAATTTATAGT"))
cat(find_seq(seq, tolower(s)), sep="\t", s, "\n")
}
paste. <- function (..., digits=16, sep=" ", collapse=NULL) {
args <- list(...)
if (length(args) == 0)
if (length(collapse) == 0) character(0)
else ""
else {
for(i in seq(along=args))
if(is.numeric(args[[i]]))
args[[i]] <- as.character(round(args[[i]], digits))
else args[[i]] <- as.character(args[[i]])
.Internal(paste(args, sep, collapse))
}
}
file <- paste("../fasta/fasta", n, ".txt", sep="")
knucleotide_brute_2(file)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
# The program reads ../fasta/fasta<n>.txt, so write that input with this
# suite's fasta.R into a scratch directory first, outside the timed region.
generator <- new.env()
generator$.ry_bench_loading <- TRUE
sys.source("fasta.R", envir = generator)
scratch <- tempfile("shootout-")
dir.create(file.path(scratch, "fasta"), recursive = TRUE)
dir.create(file.path(scratch, "run"))
capture.output(generator$execute(1500L), file = file.path(scratch, "fasta", paste0("fasta", 1500L, ".txt")))
home <- setwd(file.path(scratch, "run"))
on.exit({ setwd(home); unlink(scratch, recursive = TRUE) })
started <- proc.time()[[3]]
output <- capture.output(value <- execute(1500L))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("knucleotide_brute_2 %s %.4f\n", checksum, seconds))
})
K-nucleotide, named vector counts knucleotide_brute_3 5.52x

K-nucleotide that keeps the counts in a named integer vector, indexed and assigned by k-mer name Source

execute <- function(n=2000) {
gen_freq <- function(seq, frame) {
frame <- frame - 1L
ns <- nchar(seq) - frame
n <- 0L
cap <- 16L
freqs <- integer(cap)
for (i in 1:ns) {
subseq = substr(seq, i, i + frame)
#if (subseq %in% names(freqs))
# cnt <- freqs[[subseq]]
cnt <- freqs[subseq]
if (is.na(cnt)) {
#else {
cnt <- 0L
# ensure O(N) resizing (instead of O(N^2))
n <- n + 1L
#CTK - this optimization has no (positive) effect because the expanded space does not
#CTK have the names we will need; it has a detrimental effect, instead (even with GNU-R)
freqs[[cap <- if (cap < n) 2L * cap else cap]] <- 0L
}
freqs[[subseq]] <- cnt + 1L
}
return(freqs)
}
sort_seq <- function(seq, len) {
cnt_map <- gen_freq(seq, len)
#print(cnt_map)
#CTK needed these changes to make the benchmark work in GNU-R
#CTK attrs <- attributes(cnt_map)
#CTK fs <- unlist(attrs, use.names=FALSE)
#CTK seqs <- toupper(paste(names(attrs)))
#CTK --- added lines starting from here
fs <- cnt_map[cnt_map > 0]
seqs <- toupper(paste(names(fs)))
#CTK --- end of added lines
inds <- order(-fs, seqs)
#cat(paste(seqs[inds], fs[inds], collapse="\n"), "\n")
# cat(paste.(seqs[inds], 100 * fs[inds] / sum(fs), collapse="\n", digits=3),
cat(paste(seqs[inds], 100 * fs[inds] / sum(fs), collapse="\n"),
"\n")
}
find_seq <- function(seq, s) {
cnt_map <- gen_freq(seq, nchar(s))
#CTK if (!is.null(cnt <- attr(cnt_map, s)))
if (!is.na(cnt <- cnt_map[s]))
return(cnt)
return(0L)
}
knucleotide_brute_3 <- function(args) {
in_filename = args[[1]]
f <- file(in_filename, "r")
while (length(line <- readLines(f, n=1, warn=FALSE))) {
first_char <- substr(line, 1L, 1L)
if (first_char == '>' || first_char == ';')
if (substr(line, 2L, 3L) == 'TH')
break
}
n <- 0L
cap <- 8L
str_buf <- character(cap)
while (length(line <- scan(f, what="", nmax=1, quiet=TRUE))) {
first_char <- substr(line, 1L, 1L)
if (first_char == '>' || first_char == ';')
break
n <- n + 1L
# ensure O(N) resizing (instead of O(N^2))
str_buf[[cap <- if (cap < n) 2L * cap else cap]] <- ""
str_buf[[n]] <- line
}
length(str_buf) <- n
close(f)
seq <- paste(str_buf, collapse="")
for (frame in 1:2)
sort_seq(seq, frame)
for (s in c("GGT", "GGTA", "GGTATT", "GGTATTTTAATT", "GGTATTTTAATTTATAGT"))
cat(find_seq(seq, tolower(s)), sep="\t", s, "\n")
}
paste. <- function (..., digits=16, sep=" ", collapse=NULL) {
args <- list(...)
if (length(args) == 0)
if (length(collapse) == 0) character(0)
else ""
else {
for(i in seq(along=args))
if(is.numeric(args[[i]]))
args[[i]] <- as.character(round(args[[i]], digits))
else args[[i]] <- as.character(args[[i]])
.Internal(paste(args, sep, collapse))
}
}
file <- paste("../fasta/fasta", n, ".txt", sep="")
knucleotide_brute_3(file)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
# The program reads ../fasta/fasta<n>.txt, so write that input with this
# suite's fasta.R into a scratch directory first, outside the timed region.
generator <- new.env()
generator$.ry_bench_loading <- TRUE
sys.source("fasta.R", envir = generator)
scratch <- tempfile("shootout-")
dir.create(file.path(scratch, "fasta"), recursive = TRUE)
dir.create(file.path(scratch, "run"))
capture.output(generator$execute(1000L), file = file.path(scratch, "fasta", paste0("fasta", 1000L, ".txt")))
home <- setwd(file.path(scratch, "run"))
on.exit({ setwd(home); unlink(scratch, recursive = TRUE) })
started <- proc.time()[[3]]
output <- capture.output(value <- execute(1000L))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("knucleotide_brute_3 %s %.4f\n", checksum, seconds))
})
Mandelbrot, vectorized rows mandelbrot_ascii 1.58x

Iterates complex vectors one 1,300-point row at a time, packs the bits into raw bytes and prints them Source

execute <- function(n=300) {
lim <- 2
iter <- 50
mandelbrot_ascii <- function(args) {
n = if (length(args)) as.integer(args[[1]]) else 200L
n_mod8 = n %% 8L
pads <- if (n_mod8) rep.int(0, 8L - n_mod8) else integer(0)
p <- rep(as.integer(rep.int(2, 8) ^ (7:0)), length.out=n)
cat("P4\n")
cat(n, n, "\n")
for (y in 0:(n-1)) {
c <- 2 * 0:(n-1) / n - 1.5 + 1i * (2 * y / n - 1)
z <- rep(0+0i, n)
i <- 0L
while (i < iter) { # faster than for loop
z <- z * z + c
i <- i + 1L
}
bits <- as.integer(abs(z) <= lim)
bytes <- as.raw(colSums(matrix(c(bits * p, pads), 8L)))
cat(bytes,"\n")
}
}
mandelbrot_ascii(n)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(1300))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("mandelbrot_ascii %s %.4f\n", checksum, seconds))
})
Mandelbrot, scalar points mandelbrot_naive_ascii 2.71x

Iterates each point as a complex scalar with an escape test, packing bits into bytes one point at a time Source

execute <- function(n=200) {
lim <- 2
iter <- 50
mandelbrot_naive_ascii <- function(args) {
n = if (length(args)) as.integer(args[[1]]) else 200L
cat("P4\n")
cat(n, n, "\n")
for (y in 0:(n-1)) {
bits <- 0L
x <- 0L
while (x < n) {
c <- 2 * x / n - 1.5 + 1i * (2 * y / n - 1)
z <- 0+0i
i <- 0L
while (i < iter && abs(z) <= lim) {
z <- z * z + c
i <- i + 1L
}
bits <- 2L * bits + as.integer(abs(z) <= lim)
if ((x <- x + 1L) %% 8L == 0) {
cat(as.raw(bits), "\n")
bits <- 0L
}
}
xmod <- x %% 8L
if (xmod)
cat(as.raw(bits * as.integer(2^(8L - xmod))), "\n")
}
}
mandelbrot_naive_ascii(n)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(300))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("mandelbrot_naive_ascii %s %.4f\n", checksum, seconds))
})
Mandelbrot, vectorized rows, no output mandelbrot_noout 1.60x

The vectorized-row Mandelbrot without printing the bitmap, so the time is complex vector arithmetic Source

execute <- function(n=400) {
lim <- 2
iter <- 50
mandelbrot_noout <- function(args) {
n = if (length(args)) as.integer(args[[1]]) else 200L
n_mod8 = n %% 8L
pads <- if (n_mod8) rep.int(0, 8L - n_mod8) else integer(0)
p <- rep(as.integer(rep.int(2, 8) ^ (7:0)), length.out=n)
cat("P4\n")
cat(n, n, "\n")
#bin_con <- pipe("cat", "wb")
for (y in 0:(n-1)) {
c <- 2 * 0:(n-1) / n - 1.5 + 1i * (2 * y / n - 1)
z <- rep(0+0i, n)
i <- 0L
while (i < iter) { # faster than for loop
z <- z * z + c
i <- i + 1L
}
bits <- as.integer(abs(z) <= lim)
bytes <- as.raw(colSums(matrix(c(bits * p, pads), 8L)))
}
}
mandelbrot_noout(n)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(1200))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("mandelbrot_noout %s %.4f\n", checksum, seconds))
})
Mandelbrot, whole-plane matrix mandelbrot_noout_naive 1.36x

Iterates a 1,000 x 1,000 complex matrix 20 times, storing exp(-abs(Z)) into slices of a 3-D array Source

execute <- function(n=500) {
lim <- 2
iter <- 50
mandelbrot_noout_naive <- function(args) {
n = if (length(args)) as.integer(args[[1]]) else 200L
n_mod8 = n %% 8L
pads <- if (n_mod8) rep.int(0, 8L - n_mod8) else integer(0)
p <- rep(as.integer(rep.int(2, 8) ^ (7:0)), length.out=n)
cat("P4\n")
cat(n, n, "\n")
C <- matrix(0, n, n)
for (y in 0:(n-1)) {
C[, y] <- 2 * 0:(n-1) / n - 1.5 + 1i * (2 * y / n - 1)
}
m <- n
Z <- 0 # initialize Z to zero
X <- array(0, c(m,m,20)) # initialize output 3D array
for (k in 1:20) { # loop with 20 iterations
Z <- Z^2+C # the central difference equation
X[,,k] <- exp(-abs(Z)) # capture results
}
}
mandelbrot_noout_naive(n)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(1000))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("mandelbrot_noout_naive %s %.4f\n", checksum, seconds))
})
N-body nbody 1.87x

Simulates five planets with one coordinate vector per axis, using outer() and %*% at each of 40,000 steps Source

execute <- function(n=25000) {
pi <- 3.141592653589793
solar_mass <- 4 * pi * pi
days_per_year <- 365.24
n_bodies <- 5
body_x <- c(
0, # sun
4.84143144246472090e+00, # jupiter
8.34336671824457987e+00, # saturn
1.28943695621391310e+01, # uranus
1.53796971148509165e+01 # neptune
)
body_y <- c(
0, # sun
-1.16032004402742839e+00, # jupiter
4.12479856412430479e+00, # saturn
-1.51111514016986312e+01, # uranus
-2.59193146099879641e+01 # neptune
)
body_z <- c(
0, # sun
-1.03622044471123109e-01, # jupiter
-4.03523417114321381e-01, # saturn
-2.23307578892655734e-01, # uranus
1.79258772950371181e-01 # neptune
)
body_vx <- c(
0, # sun
1.66007664274403694e-03 * days_per_year, # jupiter
-2.76742510726862411e-03 * days_per_year, # saturn
2.96460137564761618e-03 * days_per_year, # uranus
2.68067772490389322e-03 * days_per_year # neptune
)
body_vy <- c(
0, # sun
7.69901118419740425e-03 * days_per_year, # jupiter
4.99852801234917238e-03 * days_per_year, # saturn
2.37847173959480950e-03 * days_per_year, # uranus
1.62824170038242295e-03 * days_per_year # neptune
)
body_vz <- c(
0, # sun
-6.90460016972063023e-05 * days_per_year, # jupiter
2.30417297573763929e-05 * days_per_year, # saturn
-2.96589568540237556e-05 * days_per_year, # uranus
-9.51592254519715870e-05 * days_per_year # neptune
)
body_mass <- c(
solar_mass, # sun
9.54791938424326609e-04 * solar_mass, # jupiter
2.85885980666130812e-04 * solar_mass, # saturn
4.36624404335156298e-05 * solar_mass, # uranus
5.15138902046611451e-05 * solar_mass # neptune
)
offset_momentum <- function() {
body_vx[[1]] <<- -sum(body_vx * body_mass) / solar_mass
body_vy[[1]] <<- -sum(body_vy * body_mass) / solar_mass
body_vz[[1]] <<- -sum(body_vz * body_mass) / solar_mass
}
advance <- function(dt) {
dxx <- outer(body_x, body_x, "-") # ~2x faster then nested for loops
dyy <- outer(body_y, body_y, "-")
dzz <- outer(body_z, body_z, "-")
distance <- sqrt(dxx * dxx + dyy * dyy + dzz * dzz)
mag <- dt / (distance * distance * distance) # ~fast as distance^3
diag(mag) <- 0
body_vx <<- body_vx - as.vector((dxx * mag) %*% body_mass)
body_vy <<- body_vy - as.vector((dyy * mag) %*% body_mass)
body_vz <<- body_vz - as.vector((dzz * mag) %*% body_mass)
body_x <<- body_x + dt * body_vx
body_y <<- body_y + dt * body_vy
body_z <<- body_z + dt * body_vz
}
energy <- function() {
dxx <- outer(body_x, body_x, "-")
dyy <- outer(body_y, body_y, "-")
dzz <- outer(body_z, body_z, "-")
distance <- sqrt(dxx * dxx + dyy * dyy + dzz * dzz)
q <- (body_mass %o% body_mass) / distance
return(sum(0.5 * body_mass *
(body_vx * body_vx + body_vy * body_vy + body_vz * body_vz)) -
sum(q[upper.tri(q)]))
}
nbody <- function(args) {
n = if (length(args)) as.integer(args[[1]]) else 1000L
options(digits=9)
offset_momentum()
cat(energy(), "\n")
for (i in 1:n)
advance(0.01)
cat(energy(), "\n")
}
nbody(n)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(40000))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("nbody %s %.4f\n", checksum, seconds))
})
N-body, matrix state nbody_2 1.82x

N-body with positions in 3 x 5 matrices and pairwise distances filled into a 3-D array by nested loops, then aperm Source

execute <- function(n=12000) {
pi <- 3.141592653589793
solar_mass <- 4 * pi * pi
days_per_year <- 365.24
n_bodies <- 5
body_r <- matrix(c(
c( # sun
0,
0,
0),
c( # jupiter
4.84143144246472090e+00,
-1.16032004402742839e+00,
-1.03622044471123109e-01),
c( # saturn
8.34336671824457987e+00,
4.12479856412430479e+00,
-4.03523417114321381e-01),
c( # uranus
1.28943695621391310e+01,
-1.51111514016986312e+01,
-2.23307578892655734e-01),
c( # neptune
1.53796971148509165e+01,
-2.59193146099879641e+01,
1.79258772950371181e-01)
), 3)
body_v <- matrix(c(
c( #sun
0,
0,
0),
c( # jupiter
1.66007664274403694e-03 * days_per_year,
7.69901118419740425e-03 * days_per_year,
-6.90460016972063023e-05 * days_per_year),
c( # saturn
-2.76742510726862411e-03 * days_per_year,
4.99852801234917238e-03 * days_per_year,
2.30417297573763929e-05 * days_per_year),
c( # uranus
2.96460137564761618e-03 * days_per_year,
2.37847173959480950e-03 * days_per_year,
-2.96589568540237556e-05 * days_per_year),
c( # neptune
2.68067772490389322e-03 * days_per_year,
1.62824170038242295e-03 * days_per_year,
-9.51592254519715870e-05 * days_per_year)
), 3)
body_mass <- c(
solar_mass, # sun
9.54791938424326609e-04 * solar_mass, # jupiter
2.85885980666130812e-04 * solar_mass, # saturn
4.36624404335156298e-05 * solar_mass, # uranus
5.15138902046611451e-05 * solar_mass # neptune
)
offset_momentum <- function() {
body_v[, 1] <<- -(body_v %*% body_mass) / solar_mass
}
advance <- function(dt) {
drr <- array(dim=c(n_bodies, n_bodies, 3))
for (i in 1:n_bodies) {
for (j in 1:n_bodies) {
drr[i, j,] <- body_r[,i] - body_r[,j]
}
}
distance <- sqrt(t(colSums(aperm(drr * drr))))
mag <- dt / (distance * distance * distance) # ~fast as distance^3
diag(mag) <- 0
for (d in 1:3)
body_v[d,] <<- body_v[d,] - as.vector((drr[,,d] * mag) %*% body_mass)
body_r <<- body_r + dt * body_v
}
energy <- function() {
drr <- array(dim=c(n_bodies, n_bodies, 3))
for (i in 1:n_bodies) {
for (j in 1:n_bodies)
drr[i, j,] <- body_r[,i] - body_r[,j]
}
distance <- sqrt(t(colSums(aperm(drr * drr))))
q <- (body_mass %o% body_mass) / distance
return(sum(0.5 * body_mass * colSums(body_v * body_v)) -
sum(q[upper.tri(q)]))
}
nbody_2 <- function(args) {
n = if (length(args)) as.integer(args[[1]]) else 1000L
options(digits=9)
offset_momentum()
cat(energy(), "\n")
for (i in 1:n)
advance(0.01)
cat(energy(), "\n")
}
nbody_2(n)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(20000))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("nbody_2 %s %.4f\n", checksum, seconds))
})
Pi digits pidigits 2.78x

Prints 55 digits of pi with a spigot over a hand-written bignum on base-10,000 integer vectors Source

# Constructors and string conversion functions
execute <- function(n=30) {
zero_mag = integer(0)
zero = c(0L, zero_mag) # ( = 0L )
one_mag = c(1L)
one = c(1L, one_mag)
ten_mag = c(10L)
ten = c(1L, ten_mag)
elem_max = 10000
elem_digits = as.integer(log10(elem_max))
signum <- function(v) v[[1]]
mag <- function(v) v[2:length(v)]
str_to_mag <- function(s) {
strip_leading_zeros <- function(s) {
for (i in 1:nchar(s))
if (substr(s, i, i) != '0')
return(substr(s, i, nchar(s)))
return("")
}
len = nchar(s <- strip_leading_zeros(s))
if (len == 0)
return(zero_mag)
mod <- len %% elem_digits
if (mod > 0) chunks <- substr(s, 1, mod)
else chunks <- character(0)
if (1 + mod < len) {
chunk_inds_begin <- seq(1 + mod, len, elem_digits)
chunk_inds_end <- chunk_inds_begin + elem_digits - 1
chunks <- c(chunks, substring(s, chunk_inds_begin, chunk_inds_end))
}
return(sapply(chunks, as.integer))
}
bigint <- function(s, i=NA) {
if (!is.na(i)) {
s <- as.character(i)
}
if (substr(s, 1, 1) == '-') {
mag <- str_to_mag(substr(s, 2L, nchar(s)))
sgn <- -1L
} else {
mag <- str_to_mag(s)
sgn <- 1L
}
return(c(if (length(mag)) sgn else 0L, mag))
}
to_int <- function(x) {
ret <- 0L
if ((x_len = length(x)) > 1L)
for (i in length(x):2)
ret <- ret * elem_max + x[[i]]
return(x[[1]] * ret)
}
mag_to_str <- function(x) {
len = length(x)
if (len == 1) return(as.character(x[[1]]))
xs <- sapply(x[2:length(x)], function(e) zeropad(e, 4))
x1 <- x[[1]]
return(paste(collapse="", sep="", c(
x[[1]],
sapply(x[2:length(x)], function(e) zeropad(e, elem_digits)))))
}
to_str <- function(x) {
if (x[[1]] == 0L)
return("0")
return(paste(sep="", if (x[[1]] < 0L) '-' else '', mag_to_str(mag(x))))
}
# Comparison functions
cmp_elem <- function(x, y) (x > y) - (x < y)
cmp_mag <- function(x, y) {
x_len = length(x)
y_len = length(y)
if (x_len < y_len) return(-1L)
if (x_len > y_len) return(1L)
for (i in 1:x_len)
if (c <- cmp_elem(x[[i]], y[[i]]))
return(c)
return(0L)
}
cmp <- function(x, y) {
x_sign = x[[1]]
y_sign = y[[1]]
if (x_sign == y_sign) {
if (x[[1]] == 1L) return(cmp_mag(mag(x), mag(y)))
if (x[[1]] == -1L) return(cmp_mag(mag(y), mag(x)))
return(0L)
}
return((x_sign > y_sign) - (x_sign < y_sign))
}
eq <- function(x, y) cmp(x, y) == 0L
ne <- function(x, y) cmp(x, y) != 0L
le <- function(x, y) cmp(x, y) <= 0L
lt <- function(x, y) cmp(x, y) < 0L
ge <- function(x, y) cmp(x, y) >= 0L
gt <- function(x, y) cmp(x, y) > 0L
# Arithmetic operations
add_mag <- function(x, y) {
# if x is shorter, swap the two vectors
if (length(x) < length(y)) {
tmp <- x; x <- y; y <- tmp
}
x_len = length(x)
y_len = length(y)
x_index <- x_len
y_index <- y_len
result <- integer(x_len)
sum <- 0L
# add common parts of both numbers
while (y_index > 0L) {
sum <- (x[[x_index]] %% elem_max + y[[y_index]] %% elem_max +
sum %/% elem_max) # TODO shift
result[[x_index]] <- sum %% elem_max
x_index <- x_index - 1L
y_index <- y_index - 1L
}
# copy remainder of the longer number while carry propagation is required
carry <- (sum >= elem_max)
while (x_index > 0L & carry) {
carry <- (result[[x_index]] <- x[[x_index]] + 1L) == 0L
x_index <- x_index - 1L
}
# copy remainder of the longer number
while (x_index > 0L) {
result[[x_index]] <- x[[x_index]]
x_index <- x_index - 1L
}
# grow result if necessary
if (carry)
return(c(0x01L, result))
return(result)
}
add <- function(x, y) {
if (y[[1]] == 0L)
return(x)
if (x[[1]] == 0L)
return(y)
if (x[[1]] == y[[1]])
return(c(x[[1]], add_mag(mag(x), mag(y))))
c <- cmp_mag(mag(x), mag(y))
if (c == 0L)
return(zero)
if (c > 0L)
result_mag <- sub_mag(mag(x), mag(y))
else
result_mag <- sub_mag(mag(y), mag(x))
return(c(sign_prod(c, x[[1]]),
strip_leading_zero_elems(result_mag)))
}
sub_mag <- function(big, little) {
big_len = length(big)
little_len = length(little)
result <- integer(big_len)
big_index <- big_len
little_index <- little_len
difference <- 0L
# subtract common parts of both numbers
while (little_index > 0L) {
difference <- (big[[big_index]] - little[[little_index]] +
if (difference < 0L) -1L else 0L)
result[[big_index]] <- difference %% elem_max
big_index <- big_index - 1L
little_index <- little_index - 1L
}
# subtract remainder of longer number while borrow propagates
borrow <- if (difference < 0L) -1 else 0L
while (big_index > 0L && borrow) {
borrow <- (result[[big_index]] <- big[[big_index]] - 1L) == -1L
big_index <- big_index - 1L
}
# copy remainder of the longer number
while (big_index > 0L) {
result[[big_index]] <- big[[big_index]]
big_index <- big_index - 1L
}
return(result)
}
sub <- function(x, y) {
if (y[[1]] == 0L)
return(x)
if (x[[1]] == 0L)
return(negate(y))
if (x[[1]] != y[[1]])
return(c(x[[1]], add_mag(mag(x), mag(y))))
c <- cmp_mag(mag(x), mag(y))
if (c == 0L)
return(zero)
if (c > 0L)
result_mag <- sub_mag(mag(x), mag(y))
else
result_mag <- sub_mag(mag(y), mag(x))
return(c(sign_prod(c, x[[1]]),
strip_leading_zero_elems(result_mag)))
}
negate <- function(x) c(-x[[1]], x[2:length(x)])
multiply_mag <- function(x, y) {
x_len = length(x)
y_len = length(y)
x_start = x_len # remove
y_start = y_len # remove
c <- integer(x_len + y_len)
carry <- 0L
k <- y_start + x_start
for (j in seq(y_start, 1L, -1L)) {
product = y[[j]] * x[[x_start]] + carry
c[k] <- product %% elem_max
carry <- product %/% elem_max
k <- k - 1L
}
c[x_start] <- carry
if (x_len <= 1)
return(c)
for (i in seq(x_start - 1L, 1L, -1L)) {
carry <- 0
k <- y_start + i
for (j in seq(y_start, 1L, -1L)) {
product = y[[j]] * x[[i]] + c[[k]] + carry
c[[k]] <- product %% elem_max
carry <- product %/% elem_max
k <- k - 1L
}
c[[i]] <- carry %% elem_max
}
return(c)
}
mul <- function(x, y) {
if (y[[1]] == 0L || x[[1]] == 0)
return(zero)
return(c(sign_prod(x[[1]], y[[1]]),
strip_leading_zero_elems(multiply_mag(mag(x), mag(y)))))
}
div_mag <- function(x_mag, y_mag) {
if (length(y_mag) == 1L && y_mag == one_mag)
return(x_mag)
x <- c(1L, x_mag)
y <- c(1L, y_mag)
x_mag_log10 = log10_mag(x_mag); y_mag_log10 = log10_mag(y_mag)
lo_log10 = x_mag_log10 - y_mag_log10 - (x_mag_log10 != y_mag_log10)
hi_log10 <- x_mag_log10 - y_mag_log10 + 1L
lo <- bigint_pow10(lo_log10)
# try pruning hi > 10 or lo <= 10
if (lo_log10 == 0L && hi_log10 > 1L) {
lo10 = mul(lo, ten)
if (gt(mul(lo10, y), x))
hi <- lo10
else {
lo <- lo10
hi <- bigint_pow10(hi_log10)
}
} else
hi <- bigint_pow10(hi_log10)
while (lt(lo, hi)) {
mid <- div2(add(add(lo, hi), one))
if (le(mul(mid, y), x))
lo <- mid
else
hi <- sub(mid, one)
}
return(mag(lo))
}
div <- function(x, y) {
# check if division by zero
if (x[[1]] == 0L)
return(zero)
c <- cmp_mag(mag(x), mag(y))
if (c == 0L)
return(one)
if (c < 0L)
return(zero)
return(c(sign_prod(x[[1]], y[[1]]), div_mag(mag(x), mag(y))))
}
# Helper arithmetic functions
div2_mag <- function(x) {
x_len <- length(x)
if (x_len == 1L)
return(x %/% 2L)
borrow <- (x[[1]] == 1)
x_start <- borrow + 1L
x_end <- x_len
result_index <- 1L
result <- integer(x_end - x_start + 1L)
for (x_index in x_start:x_end) {
d = x[[x_index]] + elem_max * borrow
result[[result_index]] <- d %/% 2
borrow <- d %% 2
result_index <- result_index + 1L
}
return(result)
}
div2 <- function(x) {
if (x[[1]] == 0L || (length(x) == 2L && x[[2]] == 1L))
return(zero)
return(c(x[[1]], div2_mag(mag(x))))
}
log10_mag <- function(m) elem_digits * (length(m) - 1L) + as.integer(log(m[[1]], 10))
bigint_pow10 <- function(n) c(1L, as.integer(10^(n %% elem_digits)), rep.int(0L, n %/% elem_digits))
# Misc functions
sign_prod <- function(x, y) (x == y) - (x != y)
strip_leading_zero_elems <- function(x) {
for (i in 1:length(x))
if (x[[i]] != 0L)
return(x[i:length(x)])
return(zero_mag)
}
zeropad <- function(s, n)
paste(sep="", paste(collapse="", rep('0', max(0L, n - nchar(s)))), s)
# PIDIGITS program
pidigits <- function(args) {
N = if (length(args)) as.integer(args[[1]]) else 100L
ONE = one
TWO = bigint("2")
TEN = bigint("10")
THREE = add(ONE, TWO)
i <- k <- ns <- 0L
k1 <- 1L
a <- t <- u <- bigint("0")
n <- d <- bigint("1")
while (TRUE) {
k <- k + 1L
t <- mul(n, TWO)
n <- mul(n, bigint(k))
a <- add(a, t)
k1 <- k1 + 2L
k1_big <- bigint(i=k1)
a <- mul(a, k1_big)
d <- mul(d, k1_big)
if (ge(a, n)) {
n3a <- add(mul(n, THREE), a)
t <- div(n3a, d)
td = mul(t, d)
u <- add(sub(n3a, td), n)
if (gt(d, u)) {
ns <- ns * 10L + to_int(t)
i <- i + 1L
if (i %% 5L == 0L) {
cat(zeropad(as.character(ns), 5))
if (i %% 2L == 0L)
cat(sep="", "\t:", i, "\n")
ns = 0L
}
if (i >= N)
break
a <- sub(a, td) # TODO use td
a <- mul(a, TEN)
n <- mul(n, TEN)
}
}
}
}
pidigits(n)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(55))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("pidigits %s %.4f\n", checksum, seconds))
})
Regex DNA regexdna 3.07x

Counts nine DNA patterns with gregexpr and expands IUB codes with gsub over one large string Source

execute <- function(n=500000L) {
pattern1 <- c(
"agggtaaa|tttaccct",
"[cgt]gggtaaa|tttaccc[acg]",
"a[act]ggtaaa|tttacc[agt]t",
"ag[act]gtaaa|tttac[agt]ct",
"agg[act]taaa|ttta[agt]cct",
"aggg[acg]aaa|ttt[cgt]ccct",
"agggt[cgt]aa|tt[acg]accct",
"agggta[cgt]a|t[acg]taccct",
"agggtaa[cgt]|[acg]ttaccct")
pattern2 <- matrix(c(
c("B", "(c|g|t)"),
c("D", "(a|g|t)"),
c("H", "(a|c|t)"),
c("K", "(g|t)"),
c("M", "(a|c)"),
c("N", "(a|c|g|t)"),
c("R", "(a|g)"),
c("S", "(c|g)"),
c("V", "(a|c|g)"),
c("W", "(a|t)"),
c("Y", "(c|t)")
), ncol=2, byrow=TRUE)
match_count <- function(ms) {
l <- length(ms[[1]])
fst <- ms[[1]][[1]]
return(if (l > 1) l else if (fst != -1L) fst else 0)
}
regexdna <- function(args) {
in_filename = args[[1]]
f <- file(in_filename, "r")
str <- paste(c(readLines(f), ""), collapse="\n")
close(f)
len1 <- nchar(str)
str <- gsub(">.*\n|\n", "", str, perl=TRUE, useBytes=TRUE)
len2 <- nchar(str)
for (pat in pattern1)
cat(pat, match_count(gregexpr(pat, str, useBytes=TRUE)), "\n")
for (i in 1:nrow(pattern2))
str <- gsub(pattern2[[i, 1]], pattern2[[i, 2]], str, perl=TRUE,
useBytes=TRUE)
cat("", len1, len2, nchar(str), sep="\n")
}
file <- paste("../fasta/fasta", n, ".txt", sep="")
regexdna(file)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
# The program reads ../fasta/fasta<n>.txt, so write that input with this
# suite's fasta.R into a scratch directory first, outside the timed region.
generator <- new.env()
generator$.ry_bench_loading <- TRUE
sys.source("fasta.R", envir = generator)
scratch <- tempfile("shootout-")
dir.create(file.path(scratch, "fasta"), recursive = TRUE)
dir.create(file.path(scratch, "run"))
capture.output(generator$execute(300000L), file = file.path(scratch, "fasta", paste0("fasta", 300000L, ".txt")))
home <- setwd(file.path(scratch, "run"))
on.exit({ setwd(home); unlink(scratch, recursive = TRUE) })
started <- proc.time()[[3]]
output <- capture.output(value <- execute(300000L))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("regexdna %s %.4f\n", checksum, seconds))
})
Reverse complement reversecomplement 7.42x

Reads a FASTA file line by line and maps each line's characters through a named vector in one vectorized lookup Source

execute <- function(n=150000L) {
codes <- c(
"A", "C", "G", "T", "U", "M", "R", "W", "S", "Y", "K", "V", "H", "D", "B",
"N")
complements <- c(
"T", "G", "C", "A", "A", "K", "Y", "W", "S", "R", "M", "B", "D", "H", "V",
"N")
comp_map <- NULL
comp_map[codes] <- complements
comp_map[tolower(codes)] <- complements
reversecomplement <- function(args) {
in_filename = args[[1]]
f <- file(in_filename, "r")
while (length(s <- readLines(f, n=1, warn=FALSE))) {
codes <- strsplit(s, split="")[[1]]
if (codes[[1]] == '>')
cat(s, "\n", sep="")
else {
cat(paste(comp_map[codes], collapse=""), "\n", sep="")
}
}
close(f)
}
file <- paste("../fasta/fasta", n, ".txt", sep="")
reversecomplement(file)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
# The program reads ../fasta/fasta<n>.txt, so write that input with this
# suite's fasta.R into a scratch directory first, outside the timed region.
generator <- new.env()
generator$.ry_bench_loading <- TRUE
sys.source("fasta.R", envir = generator)
scratch <- tempfile("shootout-")
dir.create(file.path(scratch, "fasta"), recursive = TRUE)
dir.create(file.path(scratch, "run"))
capture.output(generator$execute(80000L), file = file.path(scratch, "fasta", paste0("fasta", 80000L, ".txt")))
home <- setwd(file.path(scratch, "run"))
on.exit({ setwd(home); unlink(scratch, recursive = TRUE) })
started <- proc.time()[[3]]
output <- capture.output(value <- execute(80000L))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("reversecomplement %s %.4f\n", checksum, seconds))
})
Reverse complement, whole file reversecomplement_2 9.88x

Reverse complement that reads the whole file with one readLines call before mapping each line Source

execute <- function(n=150000L) {
codes <- c(
"A", "C", "G", "T", "U", "M", "R", "W", "S", "Y", "K", "V", "H", "D", "B",
"N")
complements <- c(
"T", "G", "C", "A", "A", "K", "Y", "W", "S", "R", "M", "B", "D", "H", "V",
"N")
comp_map <- NULL
comp_map[codes] <- complements
comp_map[tolower(codes)] <- complements
reversecomplement_2 <- function(args) {
f <- file(args[[1]], "r")
lines <- readLines(f)
for (i in 1:length(lines)) {
codes <- strsplit(lines[[i]], split="")[[1]]
if (codes[[1]] == '>')
cat(lines[[i]], "\n", sep="")
else {
cat(paste(comp_map[codes], collapse=""), "\n",
sep="")
}
}
close(f)
}
file <- paste("../fasta/fasta", n, ".txt", sep="")
reversecomplement_2(file)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
# The program reads ../fasta/fasta<n>.txt, so write that input with this
# suite's fasta.R into a scratch directory first, outside the timed region.
generator <- new.env()
generator$.ry_bench_loading <- TRUE
sys.source("fasta.R", envir = generator)
scratch <- tempfile("shootout-")
dir.create(file.path(scratch, "fasta"), recursive = TRUE)
dir.create(file.path(scratch, "run"))
capture.output(generator$execute(100000L), file = file.path(scratch, "fasta", paste0("fasta", 100000L, ".txt")))
home <- setwd(file.path(scratch, "run"))
on.exit({ setwd(home); unlink(scratch, recursive = TRUE) })
started <- proc.time()[[3]]
output <- capture.output(value <- execute(100000L))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("reversecomplement_2 %s %.4f\n", checksum, seconds))
})
Reverse complement, scalar lookup reversecomplement_naive 5.87x

Reverse complement that maps one character at a time with [[ on the named vector in a for loop Source

execute <- function(n=50000) {
codes <- c(
"A", "C", "G", "T", "U", "M", "R", "W", "S", "Y", "K", "V", "H", "D", "B",
"N")
complements <- c(
"T", "G", "C", "A", "A", "K", "Y", "W", "S", "R", "M", "B", "D", "H", "V",
"N")
comp_map <- NULL
comp_map[codes] <- complements
comp_map[tolower(codes)] <- complements
reversecomplement_naive <- function(args) {
f <- file(args[[1]], "r")
while (length(s <- readLines(f, n=1, warn=FALSE))) {
codes <- strsplit(s, split="")[[1]]
if (codes[[1]] == '>')
cat(s, "\n", sep="")
else {
for (j in 1:length(codes))
codes[[j]] <- comp_map[[codes[[j]]]]
cat(paste(codes, collapse=""), "\n", sep="")
}
}
close(f)
}
file <- paste("../fasta/fasta", n, ".txt", sep="")
reversecomplement_naive(file)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
# The program reads ../fasta/fasta<n>.txt, so write that input with this
# suite's fasta.R into a scratch directory first, outside the timed region.
generator <- new.env()
generator$.ry_bench_loading <- TRUE
sys.source("fasta.R", envir = generator)
scratch <- tempfile("shootout-")
dir.create(file.path(scratch, "fasta"), recursive = TRUE)
dir.create(file.path(scratch, "run"))
capture.output(generator$execute(100000L), file = file.path(scratch, "fasta", paste0("fasta", 100000L, ".txt")))
home <- setwd(file.path(scratch, "run"))
on.exit({ setwd(home); unlink(scratch, recursive = TRUE) })
started <- proc.time()[[3]]
output <- capture.output(value <- execute(100000L))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("reversecomplement_naive %s %.4f\n", checksum, seconds))
})
Spectral norm spectralnorm 21x

Multiplies by an 800 x 800 matrix once, filling each row with scalar closure calls and taking a %*% dot product Source

execute <- function(n=1200) {
spectralnorm <- function(args) {
n = if (length(args)) as.integer(args[[1]]) else 100L
options(digits=10)
eval_A <- function(i, j) 1 / ((i + j) * (i + j + 1) / 2 + i + 1)
eval_A_times_u <- function(u) {
ret <- double(n)
for (i in 0:n1) {
eval_A_col <- double(n)
for (j in 0:n1)
eval_A_col[[j + 1]] <- eval_A(i, j)
ret[[i + 1]] <- u %*% eval_A_col
}
return(ret)
}
eval_At_times_u <- function(u) {
ret <- double(n)
for (i in 0:n1) {
eval_At_col <- double(n)
for (j in 0:n1)
eval_At_col[[j + 1]] <- eval_A(j, i)
ret[[i + 1]] <- u %*% eval_At_col
}
return(ret)
}
eval_AtA_times_u <- function(u) eval_At_times_u(eval_A_times_u(u))
n1 <- n - 1
u <- rep(1, n)
v <- rep(0, n)
# was 10
for (itr in seq(1)) {
v <- eval_AtA_times_u(u)
u <- eval_AtA_times_u(v)
}
#
# cat(sqrt(sum(u * v) / sum(v * v)), "\n")
#
# sqrt(sum(u * v) / sum(v * v))
u
}
spectralnorm(n)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(800))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("spectralnorm %s %.4f\n", checksum, seconds))
})
Spectral norm, precomputed matrix spectralnorm_alt 2.14x

Builds the 3,000 x 3,000 matrix once with outer() and runs 10 power iterations as matrix products Source

execute <- function(n=1500) {
spectralnorm_alt <- function(args) {
n = if (length(args)) as.integer(args[[1]]) else 100L
options(digits=10)
eval_A <- function(i, j) 1 / ((i + j - 2) * (i + j - 1) / 2 + i)
eval_A_times_u <- function(u) u %*% g_eval_A_mat
eval_At_times_u <- function(u) u %*% g_eval_At_mat
eval_AtA_times_u <- function(u) eval_At_times_u(eval_A_times_u(u))
g_eval_A_mat <- outer(seq(n), seq(n), FUN=eval_A)
g_eval_At_mat <- t(g_eval_A_mat)
u <- rep(1, n)
v <- rep(0, n)
for (itr in seq(10)) {
v <- eval_AtA_times_u(u)
u <- eval_AtA_times_u(v)
}
cat(sqrt(sum(u * v) / sum(v * v)), "\n")
}
spectralnorm_alt(n)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(3000))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("spectralnorm_alt %s %.4f\n", checksum, seconds))
})
Spectral norm, matrix rebuilt per product spectralnorm_alt_2 3.05x

Spectral norm that rebuilds the 1,200 x 1,200 matrix with outer() for every matrix product Source

execute <- function(n=1200) {
spectralnorm_alt_2 <- function(args) {
n = if (length(args)) as.integer(args[[1]]) else 100L
options(digits=10)
n = if (length(args)) as.integer(args[[1]]) else 100L
eval_A <- function(i, j) 1 / ((i + j - 2) * (i + j - 1) / 2 + i)
eval_A_times_u <- function(u)
u %*% outer(seq(n), seq(n), FUN=eval_A)
eval_At_times_u <- function(u)
u %*% t(outer(seq(n), seq(n), FUN=eval_A))
eval_AtA_times_u <- function(u)
eval_At_times_u(eval_A_times_u(u))
u <- rep(1, n)
v <- rep(0, n)
for (itr in seq(10)) {
v <- eval_AtA_times_u(u)
u <- eval_AtA_times_u(v)
}
cat(sqrt(sum(u * v) / sum(v * v)), "\n")
}
spectralnorm_alt_2(n)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(1200))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("spectralnorm_alt_2 %s %.4f\n", checksum, seconds))
})
Spectral norm, element-wise matrix fill spectralnorm_alt_3 2.60x

Spectral norm that copies a precomputed table into a fresh matrix with [[i, j]]<- in nested loops for every product Source

execute <- function(n=250) {
spectralnorm_alt_3 <- function(args) {
n = if (length(args)) as.integer(args[[1]]) else 100L
options(digits=10)
eval_A <- function(i, j) eval_A_cache[[i, j]]
eval_A_times_u <- function(u) {
# eval_A_mat <- outer(seq(n), seq(n), FUN=eval_A)
eval_A_mat <- matrix(0, n, n)
for (i in 1:n)
for (j in 1:n)
eval_A_mat[[i, j]] <- eval_A(i, j)
return(u %*% t(eval_A_mat))
}
eval_At_times_u <- function(u) {
# eval_At_mat <- t(outer(seq(n), seq(n), FUN=eval_A))
eval_At_mat <- matrix(0, n, n)
for (i in 1:n)
for (j in 1:n)
eval_At_mat[[i, j]] <- eval_A(i, j)
return(u %*% eval_At_mat)
}
eval_AtA_times_u <- function(u) eval_At_times_u(eval_A_times_u(u))
eval_A_cache <- matrix(0, n, n)
for (i in 1:n)
for (j in 1:n)
eval_A_cache[[i, j]] <- 1/((i + j - 2) * (i + j - 1) / 2 + i)
u <- rep(1, n)
v <- rep(0, n)
for (itr in seq(10)) {
v <- eval_AtA_times_u(u)
u <- eval_AtA_times_u(v)
}
cat(sqrt(sum(u * v) / sum(v * v)), "\n")
}
spectralnorm_alt_3(n)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(250))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("spectralnorm_alt_3 %s %.4f\n", checksum, seconds))
})
Spectral norm, eigenvalues spectralnorm_math 1.16x

Computes the spectral norm directly as the largest eigenvalue of t(A) %*% A with eigen(), so LAPACK dominates Source

execute <- function(n=1200) {
spectralnorm_math <- function(args) {
n = if (length(args)) as.integer(args[[1]]) else 100L
options(digits=10)
eval_A <- function(i, j) 1 / ((i + j - 2) * (i + j - 1) / 2 + i)
m <- outer(seq(n), seq(n), FUN=eval_A)
cat(sqrt(max(eigen(t(m) %*% m)$val)), "\n")
}
spectralnorm_math(n)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(1000))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("spectralnorm_math %s %.4f\n", checksum, seconds))
})
Spectral norm, scalar loops spectralnorm_naive 23x

Spectral norm with every matrix-vector product as nested scalar loops that call a closure per element Source

execute <- function(n=150) {
spectralnorm_naive <- function(args) {
n = if (length(args)) as.integer(args[[1]]) else 100L
options(digits=10)
eval_A <- function(i, j) 1 / ((i + j) * (i + j + 1) / 2 + i + 1)
# TODO: get rid of this hack
eval_A_2 <- function(i, j) 1 / ((i + j) * (i + j + 1) / 2 + i + 1)
eval_A_times_u <- function(u) {
ret <- rep(0, n)
for (i in 1:n)
for (j in 0:n1)
ret[[i]] <- ret[[i]] + u[[j + 1]] * eval_A(i - 1, j)
return(ret)
}
eval_At_times_u <- function(u) {
ret <- rep(0, n)
for (i in 1:n)
for (j in 0:n1)
ret[[i]] <- ret[[i]] + u[[j + 1]] * eval_A_2(j, i - 1)
return(ret)
}
eval_AtA_times_u <- function(u) eval_At_times_u(eval_A_times_u(u))
n1 <- n - 1
u <- rep(1, n)
v <- rep(0, n)
for (itr in seq(10)) {
v <- eval_AtA_times_u(u)
u <- eval_AtA_times_u(v)
}
cat(sqrt(sum(u * v) / sum(v * v)), "\n")
}
spectralnorm_naive(n)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(250))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("spectralnorm_naive %s %.4f\n", checksum, seconds))
})

rcp micro-benchmarks and convolutions. The 13 micro-benchmarks of the simple/ directory, each one tight loop that isolates an interpreter cost such as variable assignment, vector subassignment or closure calls, and the 3 convolution programs of RealThing/, which compute the same convolution with scalar loops or vector operations. Source: github.com/PRL-PRG/r-compile-server/tree/b2b13a3150bddcc2d6dae8c37ecbbf1007170940/client/rsh/inst/benchmarks, MIT.

Many local assignments bytecodes 10x

A while loop that assigns the counter to 24 local variables per iteration, 500,000 times Source

execute <- function(n=10000) {
a <- 0
b <- 0
c <- 0
d <- 0
e <- 0
f <- 0
g <- 0
h <- 0
i <- 0
j <- 0
k <- 0
l <- 0
m <- 0
o <- 0
p <- 0
q <- 0
r <- 0
s <- 0
t <- 0
u <- 0
v <- 0
w <- 0
x <- 0
y <- 0
z <- 0
while (i < n) {
a <- i
b <- i
c <- i
d <- i
e <- i
f <- i
g <- i
h <- i
j <- i
k <- i
l <- i
m <- i
o <- i
p <- i
q <- i
r <- i
s <- i
t <- i
u <- i
v <- i
w <- i
x <- i
y <- i
z <- i
i <- i + 1
}
a + b + c + d + e + f + g + h + j + k + l + m + o + p + q + r + s + t + u + v + w + x + y + z
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(500000))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("bytecodes %s %.4f\n", checksum, seconds))
})
Convolution, scalar loops convolution 7.40x

Convolves two 1,000-element vectors 10 times with nested for loops and [ indexing, as in Writing R Extensions Source

execute <- function(n=500) {
convolve <- function(a, b) # from the extending R manual
{
a <- as.double(a)
b <- as.double(b)
na <- length(a)
nb <- length(b)
ab <- double(na + nb)
for(i in 1 : na)
for(j in 1 : nb)
ab[i + j] <- ab[i + j] + a[i] * b[j]
ab
}
a <- 1:n
b <- 1:n
checksum <- 0
for (i in 1:10) {
checksum <- checksum + convolve(a,b)[[n]]
}
# cat("Convolution ", n, " " , checksum, ": ")
checksum
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(1000))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("convolution %s %.4f\n", checksum, seconds))
})
Convolution, [[ indexing convolution_slow 5.24x

Convolution with nested loops over seq(length =), [[ indexing and the outer element hoisted into a local Source

execute <- function(n=1500) {
convolveSlow <- function(x,y) {
nx <- length(x)
ny <- length(y)
z <- numeric(nx + ny - 1)
for(i in seq(length = nx)) {
xi <- x[[i]]
for(j in seq(length = ny)) {
ij <- i + j - 1
z[[ij]] <- z[[ij]] + xi * y[[j]]
}
}
z
}
a <- 1:n
b <- 1:n
checksum <- 0
for (i in 1:10) {
checksum <- checksum + convolveSlow(a,b)[[n]]
}
# cat("Convolution ", n, " " , checksum, ": ")
checksum
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(1000))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("convolution_slow %s %.4f\n", checksum, seconds))
})
Convolution, vectorized convolution_v 1.60x

Convolves 1,500-element vectors with outer(), rbind, reshaping through length<- and dim<-, and rowSums Source

execute <- function(n=2000) {
convolveV <- function(x, y) {
nx <- length(x)
ny <- length(y)
xy <- rbind(outer(x,y), matrix(0, nx, ny))
nxy <- nx + ny - 1
length(xy) <- nxy * ny
dim(xy) <- c(nxy, ny)
rowSums(xy)
}
a <- 1:n
b <- 1:n
checksum <- 0
for (i in 1:10) {
checksum <- checksum + convolveV(a,b)[[n]]
}
# cat("Convolution ", n, " " , checksum, ": ")
checksum
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(1500))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("convolution_v %s %.4f\n", checksum, seconds))
})
Empty for loop empty-for 4.11x

Runs a for loop with an empty body over 1:1e8, measuring pure loop overhead Source

execute <- function(n=100000000) {
for (i in 1:n) {
}
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(1e8))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("empty-for %s %.4f\n", checksum, seconds))
})
Counting while loop empty-while 8.20x

Increments a double scalar in a while loop up to 2e7 Source

execute <- function(n=2000000) {
x <- 1
while (x < n) {
x <- x + 1
}
x
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(2e7))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("empty-while %s %.4f\n", checksum, seconds))
})
lapply with a closure lapply 3.47x

Calls a one-argument closure through lapply over 1e6 integers Source

execute <- function(n=1000000) {
x <- 1:n
f <- function(x) x + 1.5
lapply(x, f)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(1e6))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("lapply %s %.4f\n", checksum, seconds))
})
lapply with an extra argument lapply-dots 3.33x

Calls a two-argument closure through lapply over 1e6 integers, passing the second argument through ... Source

execute <- function(n=1000000) {
x <- 1:n
f <- function(x, y) x + 1.5 * y
a <- n / 2
lapply(x, f, y=a)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(1e6))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("lapply-dots %s %.4f\n", checksum, seconds))
})
Growing a list, for loop list-for 5.17x

Grows an empty list to 1.5e6 elements by [[<- in a for loop Source

execute <- function(n=500000) {
x <- list()
for (i in 1:n) {
x[[i]] <- i
}
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(1.5e6))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("list-for %s %.4f\n", checksum, seconds))
})
Growing a list, while loop list-while 5.09x

Grows an empty list to 1.5e6 elements by [[<- in a while loop with a double index Source

execute <- function(n=500000) {
x <- list()
i <- 1
while (i < n) {
x[[i]] <- 5
i <- i + 1
}
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(1.5e6))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("list-while %s %.4f\n", checksum, seconds))
})
Matrix element assignment matrix-for 3.26x

Assigns every element of a 3,000 x 3,000 matrix with [[i, j]]<- in nested for loops Source

execute <- function(n=2000) {
x <- matrix(nrow=n, ncol=n)
for (i in 1:n) {
for (j in 1:n) {
x[[i, j]] <- 5
}
}
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(3000))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("matrix-for %s %.4f\n", checksum, seconds))
})
Scalar assignment, for loop scalar-for 12x

Assigns the loop variable to a local in a for loop over 1:5e7 Source

execute <- function(n=100000000) {
x <- 0
for (i in 1:n) {
x <- i
}
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(5e7))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("scalar-for %s %.4f\n", checksum, seconds))
})
Scalar assignment, while loop scalar-while 14x

Assigns and increments two double scalars in a while loop, 2.5e7 times Source

execute <- function(n=25000000) {
x <- 0
i <- 0
while (i < n) {
x <- i
i <- i + 1
}
c(x, i)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(2.5e7))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("scalar-while %s %.4f\n", checksum, seconds))
})
Superassignment loop super-while 4.73x

A closure increments a variable of its enclosing function with <<- in a while loop, 6e6 times Source

execute <- function(n=2000000) {
x <- 0
f <- function() {
while (x < n) {
x <<- x + 1
}
}
f()
x
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(6e6))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("super-while %s %.4f\n", checksum, seconds))
})
Vector element assignment, for loop vector-for 3.51x

Fills a 3e7-element vector by [[<- in a for loop, the first write coercing it from logical to integer Source

execute <- function(n=15000000) {
x <- vector(length=n)
for (i in 1:n) {
x[[i]] <- i
}
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(3e7))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("vector-for %s %.4f\n", checksum, seconds))
})
Vector element assignment, while loop vector-while 6.90x

Fills a 2e7-element integer vector by [[<- in a while loop with an integer counter, then sums it Source

execute <- function(n=20000000) {
x <- integer(length=n)
i <- 1L
while (i < n) {
x[[i]] <- i
i <- i + 1L
}
sum(x)
}
# ---------------------------------------------------------------------------
# Driver added for ry's `just bench public` (not part of the upstream
# file). It runs `execute` once at a fixed size and prints one line:
# <name> <checksum of printed output and result> <seconds>
# Run it from this directory: upstream code reads sibling files by relative path.
# The guard skips the driver when another program loads this file for `execute`.
if (!exists(".ry_bench_loading")) local({
started <- proc.time()[[3]]
output <- capture.output(value <- execute(2e7))
seconds <- proc.time()[[3]] - started
text <- c(output, capture.output(str(value, vec.len = 3, digits.d = 10, give.attr = FALSE)))
bytes <- utf8ToInt(paste(text, collapse = "\n"))
checksum <- sprintf("%d:%.0f", length(bytes), sum(as.numeric(bytes) * (seq_along(bytes) %% 251 + 1)))
cat(sprintf("vector-while %s %.4f\n", checksum, seconds))
})

This suite times the test suites of packages from ry’s benchmark dependency set. Each package and every dependency it installs is pinned by version and by the SHA-256 of its source tarball (tools/bench/packages/lock.tsv), so two machines measure the same code. GNU R installs them into one library that both engines load, because ry’s libR is ABI-compatible with GNU R’s. The tests run the way R CMD check runs them, offline. Each time is the wall time of a whole process, or for a package whose tests/ holds plain scripts, the sum over one process per script, so it includes start-up and loading the package and its imports, as real use does.

The suite also counts the tests that pass, fail and raise errors on each engine. A package whose counts differ between the engines is flagged, and a package whose tests do not run to the end on an engine is marked as failing there and has no speed-up.

Package test suites
R6ry: 3.82x
briory: 3.82x
assertthatry: 3.75x
progressry: 3.73x
zoory: 3.37x
DBIry: 3.36x
abindry: 3.35x
lubridatery: 3.33x
zeallotry: 3.31x
cachemry: 3.30x
PerformanceAnalyticsry: 3.23x
memoisery: 3.22x
rappdirsry: 3.19x
lgrry: 2.82x
rlangtests differ on ry: GNU R: 4016 passed, 0 failed, 0 errors; ry: 3999 passed, 0 failed, 13 errorsry: 2.81x
stringrry: 2.70x
withrtests differ on ry: GNU R: 358 passed, 0 failed, 0 errors; ry: 357 passed, 0 failed, 0 errorsry: 2.53x
jsonlitery: 2.52x
purrrry: 2.50x
cliry: 2.39x
digestry: 2.28x
snakecasery: 2.06x
data.tablery: 1.92x
fsry: 1.79x
xtsry: 1.78x
foreachry: 1.21x
geometric meanry: 2.75x

Speed relative to GNU R on a log scale: a bar to the right of 1x is faster than GNU R, to the left slower. The line across a bar spans its 95% interval. A package marked tests differ passed a different number of tests on ry than on GNU R; the flag gives both counts.

Measured on 2026-11-16 on the ry 0.5.0 release archive (sha256 575ad8ad890b) with GNU R 4.6.2 on Intel(R) Xeon(R) Processor @ 2.10GHz, 4 cores, Linux 6.18.44-fc-v509; 10 paired rounds.

The numbers: seconds, peak memory and test counts
programGNU Rryry, speed-up
abind 1.4-81.081 s0.315 s3.35x (3.32x to 3.45x)
assertthat 0.2.11.128 s0.303 s3.75x (3.39x to 3.87x)
brio 1.1.51.169 s0.297 s3.82x (3.66x to 3.97x)
cachem 1.1.01.237 s0.377 s3.30x (3.17x to 3.36x)
cli 3.6.635.056 s14.771 s2.39x (2.34x to 2.44x)
data.table 1.18.6.151.111 s26.577 s1.92x (1.88x to 1.93x)
DBI 1.3.01.490 s0.440 s3.36x (3.16x to 3.50x)
digest 0.6.390.584 s0.251 s2.28x (2.21x to 2.38x)
foreach 1.5.212.822 s10.621 s1.21x (1.20x to 1.21x)
fs 2.1.014.347 s8.046 s1.79x (1.74x to 1.81x)
jsonlite 2.0.04.406 s1.760 s2.52x (2.45x to 2.60x)
lgr 0.5.29.517 s3.364 s2.82x (2.75x to 2.87x)
lubridate 1.9.520.956 s6.301 s3.33x (3.30x to 3.40x)
memoise 2.0.10.992 s0.309 s3.22x (3.09x to 3.37x)
PerformanceAnalytics 2.1.03.681 s1.123 s3.23x (3.16x to 3.36x)
progress 1.2.30.794 s0.212 s3.73x (3.56x to 3.93x)
purrr 1.2.215.826 s6.205 s2.50x (2.48x to 2.62x)
R6 2.6.14.981 s1.280 s3.82x (3.70x to 3.94x)
rappdirs 0.3.41.051 s0.324 s3.19x (3.18x to 3.37x)
rlang 1.3.078.565 s27.736 s2.81x (2.75x to 2.86x) (tests differ on ry: GNU R: 4016 passed, 0 failed, 0 errors; ry: 3999 passed, 0 failed, 13 errors)
snakecase 0.11.12.490 s1.225 s2.06x (1.96x to 2.12x)
stringr 1.6.05.522 s2.053 s2.70x (2.52x to 2.74x)
withr 3.0.34.992 s1.978 s2.53x (2.47x to 2.60x) (tests differ on ry: GNU R: 358 passed, 0 failed, 0 errors; ry: 357 passed, 0 failed, 0 errors)
xts 0.14.36.411 s3.625 s1.78x (1.74x to 1.80x)
zeallot 0.2.00.922 s0.277 s3.31x (3.04x to 3.47x)
zoo 1.9-12.571 s0.777 s3.37x (3.18x to 3.46x)

Each time is the median over the rounds. Each speed-up is the median of the per-round ratios, with its 95% interval in parentheses.

Measured on 2026-11-16 on the ry 0.5.0 release archive (sha256 575ad8ad890b) with GNU R 4.6.2 on Intel(R) Xeon(R) Processor @ 2.10GHz, 4 cores, Linux 6.18.44-fc-v509; 10 paired rounds.

ProgramWhat it runsry vs GNU R
abind 1.4-8
abind
Combines multidimensional arrays, with scripts that check dimnames and array slicing output3.35x
assertthat 0.2.1
assertthat
Easy pre and post assertions, tested through many small predicate calls3.75x
brio 1.1.5
brio
Basic R input and output, with tests that read and write files through C code3.82x
cachem 1.1.0
cachem
Caches R objects with automatic pruning, in memory and on disk3.30x
cli 3.6.6
cli
Helpers for command line interfaces, with snapshot tests of styled and formatted text2.39x
data.table 1.18.6.1
data.table
Extension of data.frame, with a large script suite of table operations backed by C code1.92x
DBI 1.3.0
DBI
The R database interface, a set of S4 generics, with tests of SQL quoting, interpolation and a dummy driver3.36x
digest 0.6.39
digest
Compact hash digests of R objects, with tests that compare many hash algorithms against known values2.28x
foreach 1.5.2
foreach
The foreach looping construct, with tests of iterators, result combiners, nesting and error handling1.21x
fs 2.1.0
fs
Cross-platform file system operations based on libuv, with tests that create and inspect real files1.79x
jsonlite 2.0.0
jsonlite
A JSON parser and generator, with round-trip tests over many R data types2.52x
lgr 0.5.2
lgr
A logging framework built on R6 classes, with appenders, layouts and filters2.82x
lubridate 1.9.5
lubridate
Date and time handling, with tests of parsing, time zones and period arithmetic3.33x
memoise 2.0.1
memoise
Memoisation of functions, with tests of in-memory and file system caches3.22x
PerformanceAnalytics 2.1.0
PerformanceAnalytics
Econometric tools for performance and risk analysis of xts return series3.23x
progress 1.2.3
progress
Terminal progress bars, with tests of their rendering from R and from C++3.73x
purrr 1.2.2
purrr
Functional programming tools, with tests of map functions and their error handling built on rlang and vctrs2.50x
R6 2.6.1
R6
Encapsulated classes with reference semantics, with tests of inheritance, cloning and active bindings3.82x
rappdirs 0.3.4
rappdirs
Determines where to save data, caches and logs, with tests over platform and environment variables3.19x
rlang 1.3.0
rlang
Functions for base types and core R features, with a large suite of tests on environments, quosures and conditions2.81x
snakecase 0.11.1
snakecase
Converts strings into any case, with tests that run many string transformations2.06x
stringr 1.6.0
stringr
Consistent wrappers for common string operations, built on stringi2.70x
withr 3.0.3
withr
Runs code with temporarily modified global state, with tests of options, environment variables and on.exit handlers2.53x
xts 0.14.3
xts
Extensible time series built on zoo, with tests of merging, subsetting and periodicity1.78x
zeallot 0.2.0
zeallot
Multiple, unpacking and destructuring assignment with the %<-% operator3.31x
zoo 1.9-1
zoo
S3 infrastructure for regular and irregular time series, with scripts compared against saved output3.37x

These are the three synthetic code bases (finance, data science and biostatistics) of about 300,000 lines in 500 files each, the size of the code ry is built to run. A program here is one whole code base: ry loads the package with ry load, GNU R loads it the same way, and each of the code base’s test_that() blocks, about 10,000 per code base, runs on its own. A code base’s time is the sum of its blocks’ own times, so loading the package and testthat’s per-file garbage collection are outside it. The result is checked by the number of blocks that pass, which must agree between the engines.

The suite measures ry with the JIT on and with it off. It is not part of the summary above: the code bases are generated, they depend on the packages of the corpus library, and one round takes about 22 minutes, so the suite runs its 10 rounds without extra rounds for wide intervals.

Benchmark code bases
  • ry
  • ry, JIT off
Financefinbenchry: 2.17xry, JIT off: 2.19x
Biostatisticsbiobenchry: 2.05xry, JIT off: 2.04x
Data sciencedsbenchry: 1.94xry, JIT off: 1.96x
geometric meanry: 2.05xry, JIT off: 2.06x

Speed relative to GNU R on a log scale: a bar to the right of 1x is faster than GNU R, to the left slower. The line across a bar spans its 95% interval.

Measured on 2026-11-17 on the ry 0.5.0 release archive (sha256 575ad8ad890b) with GNU R 4.6.2 on Intel(R) Xeon(R) Processor @ 2.10GHz, 4 cores, Linux 6.18.44-fc-v553; 10 paired rounds.

The numbers: seconds, speed-ups and the speed-up per test
programGNU Rryry, JIT offry, speed-upry, JIT off, speed-up
Finance204.617 s100.590 s94.436 s2.17x (2.05x to 2.18x)2.19x (2.14x to 2.21x)
Data science101.649 s52.412 s52.588 s1.94x (1.92x to 1.97x)1.96x (1.91x to 1.99x)
Biostatistics116.010 s57.993 s57.376 s2.05x (2.01x to 2.10x)2.04x (2.00x to 2.10x)

Each time is the median over the rounds. Each speed-up is the median of the per-round ratios, with its 95% interval in parentheses.

Measured on 2026-11-17 on the ry 0.5.0 release archive (sha256 575ad8ad890b) with GNU R 4.6.2 on Intel(R) Xeon(R) Processor @ 2.10GHz, 4 cores, Linux 6.18.44-fc-v553; 10 paired rounds.

The Per test table gives the geometric mean over the test blocks, so a code base counts every test alike, and the note under it names the slowest code base against the mean.

The corpus library lacks polars, Rblpapi and esgtoolkit, so the code bases run without them.

ProgramWhat it runsry vs GNU R
Finance
finbench
pricing, portfolio construction, risk and market-data adapters2.17x
Data science
dsbench
table pipelines, features, models and serving1.94x
Biostatistics
biobench
trials, survival, genetics and assay analysis2.05x

These are small programs we wrote whose time goes into arithmetic on numbers and numeric vectors: recursion, scalar loops, element loops over vectors, whole-vector arithmetic and a few kernels from the finance and biostatistics code bases. A process runs each program once cold and then five times warm, and the median warm run is its time, so the time leaves out start-up, GNU R’s byte compilation and ry’s first tier-up. Most programs also exist in typed ry, as a <name>.typed.R beside the plain R, which runs in strict mode as the ry typed column. Every engine must print the same checksum as GNU R, or the runner refuses to store the results.

Numeric kernels (ours)
  • ry
  • ry typed
Recursive Fibonaccifibtoo short on rytoo short on ry typedtoo short on ry typed, AOTtoo short on ry typed, installedry: 98xry typed: 140x
Collatz chainsinteger_looptoo short on ryry: 14xry typed: n/a
Drawdown and volatilitydrawdowntoo short on rytoo short on ry typedtoo short on ry typed, AOTtoo short on ry typed, installedry: 9.11xry typed: 52x
Scale and shiftvector_looptoo short on ryry: 8.53xry typed: n/a
Dot productarray_dottoo short on rytoo short on ry typedtoo short on ry typed, AOTtoo short on ry typed, installedwide interval on ry typedwide interval on ry typed, AOTwide interval on ry typed, installedry: 7.49xry typed: 43x
Element maparray_maptoo short on rytoo short on ry typedtoo short on ry typed, AOTtoo short on ry typed, installedry: 7.46xry typed: 44x
Counting filterarray_filtertoo short on rytoo short on ry typedtoo short on ry typed, AOTtoo short on ry typed, installedry: 7.24xry typed: 59x
Counted loop sumloop_sumtoo short on rytoo short on ry typedtoo short on ry typed, AOTtoo short on ry typed, installedry: 6.98xry typed: 21x
Element sumarray_sumtoo short on rytoo short on ry typedtoo short on ry typed, AOTtoo short on ry typed, installedry: 5.72xry typed: 29x
Pairwise distancesnested_loopstoo short on ryry: 5.67xry typed: n/a
Closures and lapplyclosurestoo short on ry typedtoo short on ry typed, AOTtoo short on ry typed, installedry: 5.27xry typed: 259x
Path simulationsimulationry: 3.32xry typed: n/a
Vector arithmeticvector_arithtoo short on rytoo short on ry typedtoo short on ry typed, AOTtoo short on ry typed, installedry: 2.97xry typed: 4.08x
Vasicek rate pathsvasicektoo short on ry typedtoo short on ry typed, AOTtoo short on ry typed, installedry: 2.47xry typed: 3.47x
Jackknife estimatejackknifery: 1.65xry typed: n/a
Log-rank statisticlog_rankry: 1.60xry typed: 2.51x
geometric meanry: 6.06xry typed: 26x

Speed relative to GNU R on a log scale: a bar to the right of 1x is faster than GNU R, to the left slower. The line across a bar spans its 95% interval. A time marked too short on a ry engine is under 0.1 s. That speed is what the suite measures, so it counts in the geometric mean. A program marked wide interval varied so much between rounds that its ratio's interval is wide; read its median with care.

Measured on 2026-11-17 on the ry 0.5.0 release archive (sha256 575ad8ad890b) with GNU R 4.6.2 on Intel(R) Xeon(R) Processor @ 2.10GHz, 4 cores, Linux 6.18.44-fc-v509; 10 paired rounds.

Recursion and scalar loops gain the most from the JIT. Kernels whose time goes into vector and statistical routines gain the least, because those routines run in C on GNU R too. Typed scalar loops and recursion gain more again, because a typed function in strict mode compiles to native code before its first call.

The numbers: seconds and peak memory
programGNU Rryry typedry typed, AOTry typed, installedry, speed-upry typed, speed-upry typed, AOT, speed-upry typed, installed, speed-up
Dot product0.201 s0.027 s0.005 s0.005 s0.005 s7.49x (7.11x to 8.03x) (too short on ry)43x (37x to 47x) (too short on ry typed, wide interval on ry typed)41x (38x to 47x) (too short on ry typed, AOT, wide interval on ry typed, AOT)41x (36x to 48x) (too short on ry typed, installed, wide interval on ry typed, installed)
Counting filter0.145 s0.020 s0.002 s0.003 s0.002 s7.24x (6.92x to 7.40x) (too short on ry)59x (57x to 60x) (too short on ry typed)57x (57x to 60x) (too short on ry typed, AOT)58x (56x to 61x) (too short on ry typed, installed)
Element map0.260 s0.035 s0.006 s0.006 s0.006 s7.46x (7.15x to 7.80x) (too short on ry)44x (41x to 45x) (too short on ry typed)42x (41x to 43x) (too short on ry typed, AOT)42x (40x to 43x) (too short on ry typed, installed)
Element sum0.435 s0.076 s0.015 s0.015 s0.015 s5.72x (5.50x to 5.84x) (too short on ry)29x (28x to 30x) (too short on ry typed)29x (28x to 30x) (too short on ry typed, AOT)29x (29x to 30x) (too short on ry typed, installed)
Closures and lapply0.760 s0.142 s0.003 s0.003 s0.003 s5.27x (5.08x to 5.45x)259x (250x to 269x) (too short on ry typed)269x (250x to 275x) (too short on ry typed, AOT)264x (258x to 272x) (too short on ry typed, installed)
Drawdown and volatility0.292 s0.032 s0.006 s0.006 s0.005 s9.11x (8.97x to 9.32x) (too short on ry)52x (50x to 54x) (too short on ry typed)50x (48x to 53x) (too short on ry typed, AOT)54x (49x to 56x) (too short on ry typed, installed)
Recursive Fibonacci0.697 s0.007 s0.005 s0.005 s0.005 s98x (89x to 102x) (too short on ry)140x (133x to 143x) (too short on ry typed)139x (131x to 142x) (too short on ry typed, AOT)140x (134x to 144x) (too short on ry typed, installed)
Collatz chains0.558 s0.040 sn/an/an/a14x (14x to 14x) (too short on ry)n/an/an/a
Jackknife estimate0.414 s0.247 sn/an/an/a1.65x (1.55x to 1.74x)n/an/an/a
Log-rank statistic0.347 s0.218 s0.139 s0.138 s0.141 s1.60x (1.58x to 1.62x)2.51x (2.42x to 2.55x)2.47x (2.42x to 2.52x)2.48x (2.46x to 2.51x)
Counted loop sum0.198 s0.029 s0.009 s0.009 s0.007 s6.98x (6.81x to 7.11x) (too short on ry)21x (20x to 22x) (too short on ry typed)22x (21x to 22x) (too short on ry typed, AOT)28x (27x to 28x) (too short on ry typed, installed)
Pairwise distances0.153 s0.028 sn/an/an/a5.67x (5.42x to 5.74x) (too short on ry)n/an/an/a
Path simulation0.449 s0.137 sn/an/an/a3.32x (3.25x to 3.35x)n/an/an/a
Vasicek rate paths0.287 s0.119 s0.083 s0.086 s0.083 s2.47x (2.34x to 2.55x)3.47x (3.33x to 3.62x) (too short on ry typed)3.33x (3.18x to 3.56x) (too short on ry typed, AOT)3.48x (3.31x to 3.63x) (too short on ry typed, installed)
Vector arithmetic0.181 s0.061 s0.045 s0.045 s0.045 s2.97x (2.82x to 3.05x) (too short on ry)4.08x (3.83x to 4.16x) (too short on ry typed)4.04x (3.86x to 4.13x) (too short on ry typed, AOT)4.00x (3.89x to 4.07x) (too short on ry typed, installed)
Scale and shift0.309 s0.037 sn/an/an/a8.53x (7.97x to 9.00x) (too short on ry)n/an/an/a

Each time is the median over the rounds. Each speed-up is the median of the per-round ratios, with its 95% interval in parentheses. ry typed, AOT compiles each typed function before the program starts, which the First run table shows. ry typed, installed installs the program's definitions as a package, with the machine code of its typed functions in the package's typed code file, and runs the rest of the program from a script that attaches the package. That script is plain R, because ry's checker does not resolve the names `library()` attaches, so each call of the measured function from it converts its arguments at the boundary.

Measured on 2026-11-17 on the ry 0.5.0 release archive (sha256 575ad8ad890b) with GNU R 4.6.2 on Intel(R) Xeon(R) Processor @ 2.10GHz, 4 cores, Linux 6.18.44-fc-v509; 10 paired rounds.

Each program as plain R and, where it exists, typed. The number beside the name is ry’s speed-up on the plain R.

Dot product array_dot 7.49x

An element loop over a million doubles summing `x[[i]] * y[[i]]`, five times: scalar indexing and arithmetic. Source

array_dot <- function(x, y, times) {
total <- 0
n <- length(x)
for (k in seq_len(times)) {
for (i in seq_len(n)) total <- total + x[[i]] * y[[i]]
}
total
}
x <- seq(0, 1, length.out = 1000000L)
y <- rev(x)
measure("array_dot", function() array_dot(x, y, 5L))
Counting filter array_filter 7.24x

An element loop over a million doubles counting those above one half: indexing and comparison in a loop. The typed program counts with `List.count_if`. Source

array_filter <- function(x, y, times) {
total <- 0
n <- length(x)
for (k in seq_len(times)) {
for (i in seq_len(n)) if (x[[i]] > 0.5) total <- total + 1
}
total
}
x <- seq(0, 1, length.out = 1000000L)
y <- rev(x)
measure("array_filter", function() array_filter(x, y, 5L))
Element map array_map 7.46x

An element loop writing `x[[i]] * 2 + 1` into a preallocated vector of a million doubles: subassignment in a loop. Source

array_map <- function(x, y, times) {
total <- 0
n <- length(x)
out <- numeric(n)
for (k in seq_len(times)) {
for (i in seq_len(n)) out[[i]] <- x[[i]] * 2 + 1
total <- total + sum(out)
}
total
}
x <- seq(0, 1, length.out = 1000000L)
y <- rev(x)
measure("array_map", function() array_map(x, y, 5L))
Element sum array_sum 5.72x

An element loop summing a million doubles, twenty times: the cheapest indexed loop. Source

array_sum <- function(x, y, times) {
total <- 0
n <- length(x)
for (k in seq_len(times)) {
for (i in seq_len(n)) total <- total + x[[i]]
}
total
}
x <- seq(0, 1, length.out = 1000000L)
y <- rev(x)
measure("array_sum", function() array_sum(x, y, 20L))
Closures and lapply closures 5.27x

Small closures called in a loop, then lapply and vapply over a sequence: closure calls and argument matching. Source

closures <- function(n) {
add <- function(a, b) a + b
scale <- 3
times_scale <- function(a) a * scale
s <- 0
for (i in 1:n) s <- add(s, times_scale(i))
squares <- lapply(seq_len(n), function(x) x * (x + 0.5))
halves <- vapply(seq_len(n), function(x) x / 2, numeric(1))
squared <- unlist(squares)
s + sum(squared) + sum(halves)
}
measure("closures", function() closures(300000L))
Drawdown and volatility drawdown 9.11x

Maximum drawdown and EWMA volatility in one pass over a return series: scalar state carried through a loop. Source

risk_report <- function(returns, lambda = 0.94) {
wealth <- 1
peak <- 1
worst <- 0
variance <- returns[1] * returns[1]
for (i in seq_along(returns)) {
r <- returns[i]
wealth <- wealth * (1 + r)
if (wealth > peak) peak <- wealth
drawdown <- (peak - wealth) / peak
if (drawdown > worst) worst <- drawdown
variance <- lambda * variance + (1 - lambda) * r * r
}
c(max_drawdown = worst, volatility = sqrt(variance * 252))
}
returns <- sin(seq_len(2000000L) * 0.37) * 0.01 + 0.0002
measure("drawdown", function() sum(risk_report(returns)))
Recursive Fibonacci fib 98x

fib(30), about 2.7 million calls of a small recursive function: call overhead. Source

fib <- function(n) if (n < 2) n else fib(n - 1) + fib(n - 2)
measure("fib", function() fib(30))
Collatz chains integer_loop 14x

Collatz chain lengths up to 30,000 in nested while loops: integer arithmetic and branches. Source

Jackknife estimate jackknife 1.65x

A jackknife standard error through vapply and a closure that drops one element: vector copies per call. Source

Log-rank statistic log_rank 1.60x

The log-rank test statistic, a loop over event times with whole-vector comparisons in its body. Source

log_rank <- function(data) {
times <- sort(unique(data$time[data$event == 1L]))
observed <- 0
expected <- 0
variance <- 0
for (t in times) {
risk <- data$time >= t
n <- sum(risk)
n1 <- sum(risk & data$arm == "treated")
d <- sum(data$time == t & data$event == 1L)
d1 <- sum(data$time == t & data$event == 1L & data$arm == "treated")
observed <- observed + d1
expected <- expected + d * n1 / n
if (n > 1) variance <- variance + d * (n1 / n) * (1 - n1 / n) * (n - d) / (n - 1)
}
(observed - expected)^2 / variance
}
subjects <- 16000L
data <- list(
time = (seq_len(subjects) * 7919L) %% 1000L + 1L,
event = as.integer(seq_len(subjects) %% 3L != 0L),
arm = ifelse(seq_len(subjects) %% 2L == 0L, "treated", "control")
)
measure("log_rank", function() log_rank(data))
Counted loop sum loop_sum 6.98x

A scalar double accumulator in a counted loop of ten million steps: the bare loop. Source

loop_sum <- function(n) {
s <- 0
for (i in 1:n) s <- s + i * 0.5
s
}
measure("loop_sum", function() loop_sum(10000000L))
Pairwise distances nested_loops 5.67x

An O(n^2) pairwise distance sum in two nested loops: scalar indexing in an inner loop. Source

Path simulation simulation 3.32x

Simulated price paths with matrix products, apply over rows, quantiles and sorting: vectorized model code. Source

Vasicek rate paths vasicek 2.47x

Vasicek interest-rate paths, one matrix row per step: vector arithmetic and matrix subassignment. Source

vasicek_paths <- function(paths = 200L, steps = 20000L, r0 = 0.03, kappa = 0.5, theta = 0.04, sigma = 0.01, dt = 1 / 12, seed = 9L) {
set.seed(seed)
rates <- matrix(r0, nrow = steps + 1L, ncol = paths)
for (t in seq_len(steps)) {
shock <- stats::rnorm(paths)
previous <- rates[t, ]
rates[t + 1L, ] <- previous + kappa * (theta - previous) * dt + sigma * sqrt(dt) * shock
}
rates
}
measure("vasicek", function() sum(vasicek_paths()))
Vector arithmetic vector_arith 2.97x

Whole-vector arithmetic on a million doubles, repeated in a loop: allocation of large temporaries. Source

vector_arith <- function(x, times) {
total <- 0
for (k in seq_len(times)) {
y <- x * 2 + x / 3 - 1
total <- total + sum(y)
}
total
}
x <- seq(0, 1, length.out = 1000000L)
measure("vector_arith", function() vector_arith(x, 20L))
Scale and shift vector_loop 8.53x

Element-wise reads and writes of a double vector in a loop: `y[i] <- x[i] * 2 + 1`. Source

Every program reports its time through harness.R, which also prints the checksum that all engines must agree on.

These are programs we wrote in the shape of the code that packages and analyses run: validation at API boundaries, condition handling, S3, S4 and R6 objects, records as named lists, string building, data frames and data.table, time series and a testthat suite. Their time goes into R’s object and data machinery rather than into arithmetic. They are measured like the kernels, and six of them also exist in typed ry.

Application workloads (ours)
  • ry
  • ry typed
Report formattingreporttoo short on rytoo short on ry typedtoo short on ry typed, AOTtoo short on ry typed, installedry: 15xry typed: 112x
String handlingstringstoo short on ryry: 8.96xry typed: n/a
Order invoicingorderstoo short on rytoo short on ry typedtoo short on ry typed, AOTtoo short on ry typed, installedry: 6.79xry typed: 531x
Argument validationvalidationry: 5.33xry typed: n/a
testthat suitetestthatry: 5.33xry typed: n/a
Bond book pricingbond_booktoo short on ry typedtoo short on ry typed, AOTtoo short on ry typed, installedry: 3.49xry typed: 25x
Object systemsobjectsry: 3.30xry typed: n/a
Records as listsrecordsry: 3.30xry typed: n/a
Sales table pipelinesalestoo short on rytoo short on ry typedtoo short on ry typed, AOTtoo short on ry typed, installedry: 3.16xry typed: 6.88x
Row validationteam_scorestoo short on ry typedtoo short on ry typed, AOTtoo short on ry typed, installedry: 2.85xry typed: 213x
Functional stylefunctionalry: 2.62xry typed: n/a
Expression treeexpr_treetoo short on ry typedtoo short on ry typed, AOTtoo short on ry typed, installedry: 2.22xry typed: 62x
Condition handlingconditionsry: 2.21xry typed: n/a
Time series and datestimeseriesry: 2.09xry typed: n/a
Tablestablesry: 1.82xry typed: n/a
geometric meanry: 3.77xry typed: 72x

Speed relative to GNU R on a log scale: a bar to the right of 1x is faster than GNU R, to the left slower. The line across a bar spans its 95% interval. A time marked too short on a ry engine is under 0.1 s. That speed is what the suite measures, so it counts in the geometric mean.

Measured on 2026-11-17 on the ry 0.5.0 release archive (sha256 575ad8ad890b) with GNU R 4.6.2 on Intel(R) Xeon(R) Processor @ 2.10GHz, 4 cores, Linux 6.18.44-fc-v509; 10 paired rounds.

These programs gain less than the kernels, because call setup, method dispatch, condition handling and complex assignment decide their time, not loops the JIT compiles. Typing gains the most where the typed core compiles the whole program. Where it refuses part of a program, that part runs in the interpreter, and the flag under the bar names what was refused.

The numbers: seconds and peak memory
programGNU Rryry typedry typed, AOTry typed, installedry, speed-upry typed, speed-upry typed, AOT, speed-upry typed, installed, speed-up
Bond book pricing0.589 s0.168 s0.023 s0.023 s0.021 s3.49x (3.34x to 3.65x)25x (24x to 26x) (too short on ry typed)26x (25x to 27x) (too short on ry typed, AOT)29x (28x to 29x) (too short on ry typed, installed)
Condition handling0.570 s0.255 sn/an/an/a2.21x (2.06x to 2.29x)n/an/an/a
Expression tree0.315 s0.141 s0.005 s0.005 s0.005 s2.22x (2.17x to 2.27x)62x (58x to 66x) (too short on ry typed)62x (58x to 67x) (too short on ry typed, AOT)67x (62x to 69x) (too short on ry typed, installed)
Functional style0.684 s0.264 sn/an/an/a2.62x (2.55x to 2.66x)n/an/an/a
Object systems0.788 s0.236 sn/an/an/a3.30x (3.21x to 3.41x)n/an/an/a
Order invoicing0.637 s0.092 s0.001 s0.001 s0.001 s6.79x (6.62x to 6.96x) (too short on ry)531x (527x to 540x) (too short on ry typed)532x (518x to 547x) (too short on ry typed, AOT)555x (541x to 563x) (too short on ry typed, installed)
Records as lists0.503 s0.155 sn/an/an/a3.30x (3.06x to 3.36x)n/an/an/a
Report formatting0.602 s0.040 s0.005 s0.005 s0.005 s15x (15x to 16x) (too short on ry)112x (109x to 116x) (too short on ry typed)111x (107x to 117x) (too short on ry typed, AOT)112x (104x to 116x) (too short on ry typed, installed)
Sales table pipeline0.117 s0.037 s0.017 s0.017 s0.017 s3.16x (2.97x to 3.22x) (too short on ry)6.88x (6.34x to 7.26x) (too short on ry typed)7.04x (6.70x to 7.22x) (too short on ry typed, AOT)7.07x (6.78x to 7.35x) (too short on ry typed, installed)
String handling0.744 s0.084 sn/an/an/a8.96x (8.37x to 9.14x) (too short on ry)n/an/an/a
Tables0.373 s0.203 sn/an/an/a1.82x (1.76x to 1.90x)n/an/an/a
Row validation0.818 s0.281 s0.004 s0.004 s0.004 s2.85x (2.79x to 3.00x)213x (207x to 218x) (too short on ry typed)211x (207x to 218x) (too short on ry typed, AOT)206x (202x to 212x) (too short on ry typed, installed)
testthat suite0.651 s0.120 sn/an/an/a5.33x (5.15x to 5.62x)n/an/an/a
Time series and dates1.228 s0.587 sn/an/an/a2.09x (2.07x to 2.14x)n/an/an/a
Argument validation0.819 s0.152 sn/an/an/a5.33x (5.11x to 5.47x)n/an/an/a

Each time is the median over the rounds. Each speed-up is the median of the per-round ratios, with its 95% interval in parentheses. ry typed, AOT compiles each typed function before the program starts, which the First run table shows. ry typed, installed installs the program's definitions as a package, with the machine code of its typed functions in the package's typed code file, and runs the rest of the program from a script that attaches the package. That script is plain R, because ry's checker does not resolve the names `library()` attaches, so each call of the measured function from it converts its arguments at the boundary.

Measured on 2026-11-17 on the ry 0.5.0 release archive (sha256 575ad8ad890b) with GNU R 4.6.2 on Intel(R) Xeon(R) Processor @ 2.10GHz, 4 cores, Linux 6.18.44-fc-v509; 10 paired rounds.

Bond book pricing bond_book 3.49x

Prices 200,000 bonds, each a short vector computation through seq_len, rep, exp and sum: many small base calls. Source

bond_price <- function(coupon, rate, years) {
times <- seq_len(years)
flows <- rep(coupon, years)
flows[[years]] <- flows[[years]] + 100
sum(flows * exp(-rate * times))
}
price_book <- function(n) {
total <- 0
for (i in seq_len(n)) {
total <- total + bond_price(2 + i %% 5L, 0.01 + (i %% 7L) * 0.003, 5L + i %% 20L)
}
round(total, 6)
}
measure("bond_book", function() price_book(200000L))
Condition handling conditions 2.21x

tryCatch, withCallingHandlers, custom condition classes, retries and on.exit around fallible steps. Source

Expression tree expr_tree 2.22x

Builds and evaluates a tree of sums, maxima and negations: recursion over nested lists. Source

build <- function(depth, seed) {
if (depth == 0L) return(list(kind = "num", value = seed %% 10L + 1))
left <- build(depth - 1L, (seed * 7L) %% 1009L + 1L)
if (seed %% 5L == 0L) return(list(kind = "neg", inner = left))
right <- build(depth - 1L, (seed * 13L) %% 1013L + 3L)
if (seed %% 2L == 0L) list(kind = "add", left = left, right = right) else list(kind = "max", left = left, right = right)
}
evaluate <- function(node) {
switch(node$kind,
num = node$value,
neg = -evaluate(node$inner),
add = evaluate(node$left) + evaluate(node$right),
max = max(evaluate(node$left), evaluate(node$right))
)
}
tree <- build(21L, 5L)
measure("expr_tree", function() evaluate(tree) + evaluate(build(14L, 8L)))
Functional style functional 2.62x

lapply, Map, Filter, Reduce, do.call and purrr over lists, function factories and memoization in an environment. Source

Object systems objects 3.30x

S3 methods with UseMethod and NextMethod, S4 classes with validity, R6 classes with active bindings. Source

Order invoicing orders 6.79x

Prices order lines with a loyalty discount and regional tax, then totals invoices: records and branches. Source

price_line <- function(order, rates) {
net <- order$quantity * order$unit
if (order$loyal) net <- net * 0.95
if (order$quantity > 5L) net <- net - 1
tax <- net * rates[[order$region]]
list(id = order$id, net = net, tax = tax, gross = net + tax)
}
price_orders <- function(n) {
regions <- c("north", "south", "east", "west")
rates <- c(north = 0.2, south = 0.1, east = 0.15, west = 0.05)
total <- 0
for (i in seq_len(n)) {
order <- list(id = i, quantity = i %% 7L + 1L, unit = 10 + (i %% 13L) * 0.5, loyal = i %% 3L == 0L, region = regions[[i %% 4L + 1L]])
invoice <- price_line(order, rates)
total <- total + invoice$gross
}
total
}
measure("orders", function() price_orders(200000L))
Records as lists records 3.30x

Named lists grown, updated with `$<-` and `[[<-`, merged with modifyList, grouped with split and bound with rbind. Source

Report formatting report 15x

Formats one line per account, then joins and measures the report: string building. Source

report <- function(n) {
lines <- character(n)
for (i in seq_len(n)) {
name <- paste0("ACC", formatC(i, width = 6L, flag = "0"))
side <- if (i %% 2L == 0L) "long" else "short"
lines[[i]] <- paste(name, sprintf("%.2f", i * 1.25), side, sep = ";")
}
text <- paste(lines, collapse = "\n")
nchar(text) + sum(nchar(toupper(lines)) > 25L) + sum(startsWith(lines, "ACC00"))
}
measure("report", function() report(20000L))
Sales table pipeline sales 3.16x

Filters a million sales rows, adds a revenue column and totals it per region: data frame indexing and tapply. Source

pipeline <- function(sales) {
big <- sales[sales$units > 3L, ]
big$revenue <- big$units * big$price
totals <- tapply(big$revenue, big$region, sum)
sum(totals * seq_along(totals))
}
i <- seq_len(1000000L)
sales <- data.frame(region = c("north", "south", "east", "west")[i %% 4L + 1L], units = i %% 17L, price = 1 + i %% 100L)
measure("sales", function() pipeline(sales))
String handling strings 8.96x

paste, sprintf, gsub, regmatches, splitting and padding, and the same through stringr. Source

Tables tables 1.82x

data.table grouping, `:=` updates, keyed joins and rbindlist, and the same steps on base data frames. Source

Row validation team_scores 2.85x

Parses comma-separated rows, rejects invalid ones and totals scores per team: strsplit, conversion and failures as values. Source

parse_row <- function(row) {
parts <- strsplit(row, ",", fixed = TRUE)[[1L]]
age <- suppressWarnings(as.integer(parts[[2L]]))
if (is.na(age)) return(list(ok = FALSE, error = "bad_age"))
if (age < 18L) return(list(ok = FALSE, error = "minor"))
list(ok = TRUE, value = list(name = parts[[1L]], age = age, score = as.double(parts[[3L]]), team = parts[[4L]]))
}
team_scores <- function(rows) {
parsed <- lapply(rows, parse_row)
totals <- list()
errors <- 0L
for (p in parsed) {
if (p$ok) {
team <- p$value$team
old <- totals[[team]]
totals[[team]] <- (if (is.null(old)) 0 else old) + p$value$score
} else {
errors <- errors + 1L
}
}
totals[["red"]] + 2 * totals[["blue"]] + 3 * totals[["green"]] + errors
}
i <- seq_len(60000L)
rows <- paste0("user", i, ",", ifelse(i %% 17L == 0L, "x", as.character(i %% 90L)), ",", (i %% 50L) / 2, ",", c("red", "blue", "green")[i %% 3L + 1L])
measure("team_scores", function() team_scores(rows))
testthat suite testthat 5.33x

Many small test_that() blocks with expect_equal, expect_error and expect_warning: testthat's condition machinery. Source

Time series and dates timeseries 2.09x

xts and zoo series merged, lagged and rolled, lubridate parsing and month arithmetic, base Date sequences. Source

Argument validation validation 5.33x

Small functions with defaults, `...` forwarding, match.arg, stopifnot, missing() and match.call(). Source

Every program reports its time through harness.R, which also prints the checksum that all engines must agree on.

The published numbers measure the build you download: the official Linux x86_64 archive of a tagged release, which is profile-guided and laid out by BOLT (tools/layout/optimize.sh). The runner unpacks the archive into a private directory where its ry extracts the runtime and base R it embeds, and the line under each chart names the release and the first digits of the archive’s sha256. A suite whose line names a commit instead was measured on a plain release build of that commit (cargo build --release), without the release layout.

A run measures several rounds. Each round runs every program on every engine once, with the programs in an order shuffled per round and the engines in an order shuffled per program, from a recorded seed. Each ratio is formed inside one round, GNU R’s time over the engine’s, and the reported speed-up is the median of those per-round ratios. Pairing matters because the measuring machines are shared cloud VMs whose effective CPU speed drifts with other tenants’ load: a slow drift moves both engines of one round together, so it cancels in the ratio. Each speed-up and each geometric mean carries a 95% bootstrap interval over the rounds.

What one timed run covers differs by suite:

Suite One process runs The time includes
Public R JIT benchmarks the program once byte compilation and JIT tier-up, not start-up
Package test suites the package’s tests start-up and loading the package and its imports
Numeric kernels, application workloads the program once cold, then five times warm the median warm run only

The runner, not this page, judges the data:

  • Checksums. Every engine must print the same checksum as GNU R for a program, or the run fails and nothing is written. A numeric checksum may differ by a relative 1e-10, because a typed program may sum in a different order. A program that fails on any engine also fails the run.
  • Package tests. The packages suite is the exception, because there the program is the package’s own test suite. Test counts that differ between the engines are flagged as tests differ, and the package stays in the geometric mean. A test script that ends in an R error, as R CMD check reads a failing script, counts as one failed test. A test suite that crashes or does not run to the end on an engine is recorded as failing there, which leaves the package out of the geometric mean.
  • Too short. A program GNU R finishes in under 0.1 s does too little work for a stable ratio, so it is flagged too short and left out of every geometric mean. A ry time under 0.1 s is flagged too, but it counts, because that speed is what the suite measures.
  • Wide intervals. A ratio whose interval reaches more than 10% from its median gets extra rounds, and it is flagged wide interval if they do not narrow it.
  • Refused typed programs. Where the typed core refuses part of a typed program, that part runs in the interpreter, and its flag gives the refusal.
SuiteReproduceBaselineEnginesSamples
Public R JIT benchmarksjust bench public --release ARCHIVE --writeGNU R 4.6.2, BLAS libRblas.soGNU R, ry, ry typed, ry typed, AOT10 paired rounds, 10 more for each wide interval, seed 1
Package test suitesjust bench packages --release ARCHIVE --writeGNU R 4.6.2, BLAS libRblas.soGNU R, ry10 paired rounds, 10 more for each wide interval, seed 1
Benchmark code basesjust bench codebases --release ARCHIVE --writeGNU R 4.6.2, BLAS libRblas.soGNU R, ry, ry, JIT off10 paired rounds, seed 1
Numeric kernels (ours)just bench kernels --release ARCHIVE --writeGNU R 4.6.2, BLAS libRblas.soGNU R, ry, ry typed, ry typed, AOT, ry typed, installed10 paired rounds, 10 more for each wide interval, seed 1
Application workloads (ours)just bench applications --release ARCHIVE --writeGNU R 4.6.2, BLAS libRblas.soGNU R, ry, ry typed, ry typed, AOT, ry typed, installed10 paired rounds, 10 more for each wide interval, seed 1

To reproduce a suite, run its command from the table on a machine with GNU R; the packages suite first needs just bench-packages install. Without --release, a run measures the local release build in target/release, which is how to compare two builds of your own (--baseline), and --ry PATH measures the release build at another path. With --write, the runner stores tools/bench/<suite>/results.json with the release tag, the archive’s file name and sha256, and the commit the tag names, and just readme rewrites the README summary from it. --write refuses a nightly archive, a local build, and a working tree with a change to a tracked file or an untracked file under crates/ or tools/bench/, so the stored programs reproduce the measurement. The README summary warns when the suites were measured on different machines (CPU model or kernel release, ignoring a Firecracker guest’s -fc-vNNN build tag) or against a GNU R on a different BLAS, because their ratios are then not comparable.