Skip to content

improve a couple benchmark implementations #283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 38 additions & 21 deletions src/problem/JSONParse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,36 +75,53 @@ function perf_parse_json(strng::AbstractString)
if strng[pos] != '"'
error("AbstractString starting with quotation expected at position $pos")
else
pos = pos + 1
pos += 1
end
str = ""
str = IOBuffer()
while pos <= len
nc = strng[pos]
if nc == '"'
pos += 1
return String(take!(str))
elseif nc == '\\'
pos = pos + 1
return string(str)
elseif nc == '\\'
if pos+1 > len
error_pos("End of file reached right after escape character")
end
pos = pos + 1
pos > len && break # goto error handling
anc = strng[pos]
if anc == '"' || anc == '\\' || anc == '/'
str = string(str, strng[pos])
pos = pos + 1
elseif anc == 'b' || anc == 'f'|| anc == 'n' || anc == 'r' || anc == 't'
str = string(str, '\\', string[pos])
pos = pos + 1
if anc == '"'
write(str, "\"")
pos += 1
elseif anc == '\\'
write(str, "\\")
pos += 1
elseif anc == '/'
write(str, "/")
pos += 1
elseif anc == 'b'
write(str, "\b")
pos += 1
elseif anc == 'f'
write(str, "\f")
pos += 1
elseif anc == 'n'
write(str, "\n")
pos += 1
elseif anc == 'r'
write(str, "\r")
pos += 1
elseif anc == 't'
write(str, "\t")
pos += 1
elseif anc == 'u'
if pos+4 > len
error_pos("End of file reached in escaped unicode character")
end
str = string(str, strng[pos-1:pos+4])
pos + 4 > len && break # goto error handling
write(str, Char(parse(Int, strng[pos:pos+4], base=16)))
pos = pos + 5
else # should rarely happen
write(str, anc)
pos = pos + 1
end
else # should never happen
str = string(str,strng[pos])
pos = pos + 1
else # common case
write(str, nc)
pos = nextind(strng, pos)
end
end
error("End of file while expecting end of string")
Expand Down
14 changes: 7 additions & 7 deletions src/shootout/binary_trees.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
#
# Ported from an OCaml version

abstract type BTree end

mutable struct Empty <: BTree
end
struct Empty end

mutable struct Node <: BTree
info
left::BTree
right::BTree
struct Node{T}
info::T
left::Union{Node{T}, Empty}
right::Union{Node{T}, Empty}
end

const BTree{T} = Union{Node{T}, Empty}

function make(val, d)
if d == 0
Node(val, Empty(), Empty())
Expand Down
69 changes: 39 additions & 30 deletions src/shootout/regex_dna.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@
# Fix from David Campbell

const variants = [
"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"
r"agggtaaa|tttaccct",
r"[cgt]gggtaaa|tttaccc[acg]",
r"a[act]ggtaaa|tttacc[agt]t",
r"ag[act]gtaaa|tttac[agt]ct",
r"agg[act]taaa|ttta[agt]cct",
r"aggg[acg]aaa|ttt[cgt]ccct",
r"agggt[cgt]aa|tt[acg]accct",
r"agggta[cgt]a|t[acg]taccct",
r"agggtaa[cgt]|[acg]ttaccct"
]

const subs = [
(r"B", "(c|g|t)"),
(r"D", "(a|g|t)"),
(r"H", "(a|c|t)"),
(r"K", "(g|t)"),
(r"M", "(a|c)"),
(r"N", "(a|c|g|t)"),
(r"R", "(a|g)"),
(r"S", "(c|g)"),
(r"V", "(a|c|g)"),
(r"W", "(a|t)"),
(r"Y", "(c|t)")
]
const subs = (
("B" => "(c|g|t)"),
("D" => "(a|g|t)"),
("H" => "(a|c|t)"),
("K" => "(g|t)"),
("M" => "(a|c)"),
("N" => "(a|c|g|t)"),
("R" => "(a|g)"),
("S" => "(c|g)"),
("V" => "(a|c|g)"),
("W" => "(a|t)"),
("Y" => "(c|t)")
)

function perf_regex_dna()
infile = joinpath(SHOOTOUT_DATA_PATH, "regexdna-input.txt")
Expand All @@ -38,20 +38,29 @@ function perf_regex_dna()
seq = replace(seq, r">.*\n|\n" => "")
l2 = length(seq)

kk = 0
for v in variants
k = 0
for m in eachmatch(Regex(v), seq)
for m in eachmatch(v, seq)
k += 1
end
# @printf("%s %d\n", v, k)
kk += k
end

for (u, v) in subs
seq = replace(seq, u => v)
try
# VERSION > 1.7-dev
seq = replace(seq, subs...)
catch ex
ex isa MethodError || rethrow()
# semi-optimized regex
r = Regex(join(first.(subs), "|"))
repl = Dict(subs)
seq = replace(seq, r => (r -> repl[r]))
## multiple passes
#for sub in subs
# seq = replace(seq, sub)
#end
end

# println()
# println(l1)
# println(l2)
# println(length(seq))
seq, kk
end