40 lines
1.2 KiB
Plaintext
40 lines
1.2 KiB
Plaintext
type InputResult {
|
|
Parsed(value: Decimal)
|
|
error Invalid
|
|
}
|
|
|
|
type State {
|
|
total: Decimal
|
|
factor: Decimal
|
|
}
|
|
|
|
type Sample {
|
|
elapsed_ns: Int
|
|
checksum: Text
|
|
}
|
|
|
|
parsed(value: Decimal) (result: Decimal) { result = value }
|
|
parsed(value: null) (result: Decimal) { fail(InputResult.Invalid) }
|
|
|
|
step(value: Int, state: State) (next: State) {
|
|
next = State {
|
|
total = state.total + decimal.fromInt(value) * state.factor
|
|
factor = state.factor
|
|
}
|
|
}
|
|
|
|
run(ticks: Text, initial: Text, factor: Text) (sample: Sample) {
|
|
bytes = dever.bytes.fromText(ticks)
|
|
state = State { total = parsed(decimal.parse(initial))
|
|
factor = parsed(decimal.parse(factor)) }
|
|
begin = dever.time.monotonicNanos()
|
|
final = reduce(step, bytes, state)
|
|
end = dever.time.monotonicNanos()
|
|
sample = Sample { elapsed_ns = end - begin
|
|
checksum = decimal.toText(final.total) }
|
|
}
|
|
|
|
continuous(ticks: Text, initial: Text, factor: Text) (samples: List<Sample>) {
|
|
samples = [run(ticks, initial, factor), run(ticks, initial, factor), run(ticks, initial, factor), run(ticks, initial, factor), run(ticks, initial, factor), run(ticks, initial, factor), run(ticks, initial, factor), run(ticks, initial, factor), run(ticks, initial, factor), run(ticks, initial, factor)]
|
|
}
|