Skip to content

Instantly share code, notes, and snippets.


Jeffrey Sarnoff JeffreySarnoff

View GitHub Profile
JeffreySarnoff / firstindex_study.jl
Last active Feb 15, 2021
benchmarking firstindex
View firstindex_study.jl
using BenchmarkTools
const BT=BenchmarkTools.DEFAULT_PARAMETERS;
BT.samples=60_000; BT.time_tolerance=1/8192;
IS{Int64} == Union{Int64, Symbol}
IS{Int32} == Union{Int32, Symbol}
const IS = Union{I,Symbol} where {I<:Integer}
# test vars, length(chrs) == 96
JeffreySarnoff /
Last active Dec 30, 2020
notes on timing an example from twitter


You are not timing the same thing in each language. See below for what you were timing in Julia.
And, as has been mentioned, use @time or @btime from BenchmarkTools.jl (time does not do what you expect it to do). When using @time, run the timing once to precompile the test before considering the results valid. The first run may take considerably longer, and that extra time is not computing time. I have omitted those lines below.

JeffreySarnoff / nancatcher.jl
Last active Dec 19, 2020
Trap NaN where generated
View nancatcher.jl
# provide context when a NaN is generated
# IMPORTANT!! define these before overloading the arithmetic functions
function nanfound(fncall)
stk = stacktrace() # stacktrace(catch_backtrace())
stk = relevantframes(stk)
info = []
for frame in stk
push!(info, frameinfo(frame))
JeffreySarnoff /
Created Jul 7, 2020
introducing the notion of an error-free arithmetic operation


An error-free transformation [EFT] is a transformation of a floating point realization of a mathematical operation that preserves the information inherent in the operation. Ordinary floating point addition of two Float64 values returns only that information about the sum which fits within a Float64 variable. For example, where flsum is short for ordinary_Float64_sum and flsum_errorfree is flsum with the remaining value that usually is lost in calculation.

flsum = 1.0 + 2.0^(-60)
flsum == 1.0

An error-free transformation of the floating point sum preserves the full value by returning two quantities.

flsum_errorfree = errorfree_sum(1.0, 2.0^(-60))
JeffreySarnoff / errorfree_arithmetic.jl
Last active Jul 7, 2020
error-free & least-error transformations with mixed precision arithmetic ops
View errorfree_arithmetic.jl
Copyright 2020 by Jeffrey Sarnoff
Released under the MIT License
Questions and Comments are Welcome.
Ask on Discourse or raise an issue:
JeffreySarnoff / rationalrev_part1.jl
Last active May 1, 2020
revising rational.jl (first part of the file)`
View rationalrev_part1.jl
Rational{T<:Integer} <: Real
Rational number type, with numerator and denominator of type `T`.
Rationals are checked for overflow.
struct Rational{T<:Integer} <: Real
JeffreySarnoff / BenchmarkingRationals.jl
Last active Apr 26, 2020
benchmarks used to compare the PR for `rational.jl` with the current `rational.jl`
View BenchmarkingRationals.jl
using LinearAlgebra
you set this directory with the const or through the environment
ensure RelativeSpeeds.jl and Fractions.jl are kept there
const DefaultBenchmarkingDir = pwd()
benchmarking_dir = haskey(ENV, "BenchmarkingDir") ? ENV["BenchmarkingDir"] : DefaultBenchmarkingDir
JeffreySarnoff / RelativeSpeeds.jl
Last active Apr 26, 2020
ascertain the performance of `fn(xs::T1)` relative to `fn(xs::T2)`
View RelativeSpeeds.jl
# enable benchmarking, force paramter presets
using BenchmarkTools
BTools.overhead = BenchmarkTools.estimate_overhead();
BTools.evals=1; BTools.samples = 7000;
BTools.time_tolerance = 1e-8;
error("BenchmarkTools.jl not found")
JeffreySarnoff / Fractions.jl
Last active Apr 26, 2020
follows PR for improving `rationals.jl`, renames `Rational` to `Fraction`, renames `//` to `⨸`
View Fractions.jl
# 2020-04-24 11:03 UTC, module follows PR for improving `rationals.jl`
# This file is a part of Julia. License is MIT:
module Fractions
export Fraction, ⨸
import Base: show, read, write, fma,
BitSigned, Bool, AbstractFloat, big, BigInt, float,
==, !=, >, >=, <, <=, isequal, isless,
JeffreySarnoff /
Last active Apr 23, 2020
summary of rational.jl improvements and questions for #triage

In Breif

Over five years, members of the community have been discussing Julia's Rational Number implementation and experimenting with modifications intended to increase the performance of Rational arithmetic while protecting correctness and catching arithmetic exceptions (overflow, underflow, division by zero).

We have an approach that speeds up correct multiplication (division). The prod of 12 rationals is faster than v1.4.1 by at least 1.5x using Rational{Int64}, 1.25x using Rational{BigInt}. In this implementation, rational addition, subtraction do not speed up.

There are several ways to approach coding the core constructor. The specifics are discussed in this PR. We ask #triage to choose which it deem the most appropriate.

The Construction Approaches