Skip to content

Commit

Permalink
Merge pull request #26 from wx257osn2/remove-trailing-spaces
Browse files Browse the repository at this point in the history
Remove Trailing Whitespaces
  • Loading branch information
reki2000 authored Jul 25, 2022
2 parents 0c90f0d + 6eb7820 commit e8b0c94
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 27 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Simple benchmarks of Dijkstra algorithm among C++, Go, Julia, Python(+Cython +Py
This benchmark uses [hyperfine](https://github.com/sharkdp/hyperfine). Follow the install instruction there.


Submodules are contained. You need to
Submodules are contained. You need to

```
git submodule update --init --recursive
Expand Down Expand Up @@ -80,10 +80,10 @@ for specific language
./run.sh [cpp|go|rust|javascript|julia|kotlin|python|cython|pypy|dart]
```

for test setup - choose one implementation (ex.`cpp`) to make a 'correct' result.
for test setup - choose one implementation (ex.`cpp`) to make a 'correct' result.
```
mkdir out
./test.sh cpp
./test.sh cpp
mv out/cpp.txt out/expected.txt
```

Expand Down
6 changes: 3 additions & 3 deletions cython/src/cy_g.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ from cython.view cimport array as cvarray
cdef class G:
cdef dict id2idx
cdef list idx2id
cdef int idx
cdef int idx
cdef list edge

def __init__(self):
self.id2idx = {}
self.idx2id = [0]
self.idx = 1
self.edge = [[]]

cdef int get_idx(G g, int id):
cdef int i
i = g.id2idx.get(id, 0)
Expand Down Expand Up @@ -71,7 +71,7 @@ cdef tuple dijkstra(G g, int DISTANCE_MULTIPLE, is_debug, int start, int end):
for i in range(size):
d[i] = MAX_INT32
prev[i] = 0

cdef list queue
queue = []
heappush(queue, (0, s))
Expand Down
2 changes: 1 addition & 1 deletion javascript/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function dijkstra(start , end ) {

const MAX_INT32 = 2147483647; // 2^31-1 - this is not the max value of javascript Number but good for this benchmark
const size = g.idx;
const d = new Array(size+1).fill(MAX_INT32);
const d = new Array(size+1).fill(MAX_INT32);
const prev = new Array(size+1).fill(0);

const queue = new pq();
Expand Down
20 changes: 10 additions & 10 deletions julia/src/main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ struct Edge
second::Distance
end

mutable struct G
mutable struct G
id2idx::Dict{NodeId,NodeIndex}
idx2id::Array{NodeId,1}
idx::NodeIndex
edge::Array{Array{Edge,1},1}
end

function get_idx!(g::G, id::NodeId)::NodeIndex
function get_idx!(g::G, id::NodeId)::NodeIndex
i = get(g.id2idx, id, 0)
if i == 0
i = g.idx
Expand All @@ -29,7 +29,7 @@ function get_idx!(g::G, id::NodeId)::NodeIndex
i
end

function add_edge!(g::G, start::NodeId, _end::NodeId, distance::Distance)
function add_edge!(g::G, start::NodeId, _end::NodeId, distance::Distance)
s = get_idx!(g, start)
e = get_idx!(g, _end)
push!(g.edge[s], Edge(e, distance))
Expand Down Expand Up @@ -94,7 +94,7 @@ function dijkstra(g::G, start::NodeId, _end::NodeId, is_debug::Bool = false)::Re
push!(queue, Visit(0, s))

visited = 0
@inbounds while ! empty(queue)
@inbounds while ! empty(queue)
a = pop!(queue)
distance = a.first
here = a.second
Expand All @@ -103,10 +103,10 @@ function dijkstra(g::G, start::NodeId, _end::NodeId, is_debug::Bool = false)::Re
end
visited = visited + 1
is_debug && println("visiting: $(here) distance: $(distance)")
for e in g.edge[here]
for e in g.edge[here]
to = e.first
w = distance + e.second
if w < d[to]
if w < d[to]
prev[to] = here
d[to] = w
push!(queue, Visit(w, to))
Expand All @@ -118,15 +118,15 @@ function dijkstra(g::G, start::NodeId, _end::NodeId, is_debug::Bool = false)::Re
n = e
result = [g.idx2id[n]]

while d[n] != typemax(Distance) && n != s && n != 0
while d[n] != typemax(Distance) && n != s && n != 0
n = prev[n]
push!(result, g.idx2id[n])
end

return Result(fld(d[e], DISTANCE_MULTIPLE), result)
end

function main()
function main()
count = parse(Int, ARGS[1])
g = G(Dict{NodeId,NodeIndex}(), NodeId[], NodeIndex(1), Vector{Edge}[])
is_debug = size(ARGS,1) > 1 && ARGS[2] == "debug"
Expand All @@ -135,7 +135,7 @@ function main()
println("loaded nodes: ", g.idx)

route = NodeId[]
for i in 1:count
for i in 1:count
s = g.idx2id[i*1000]
result = dijkstra(g, s, g.idx2id[1], is_debug)
distance = result.first
Expand All @@ -144,7 +144,7 @@ function main()
end

print("route: ")
for id in route
for id in route
print(id, " ")
end
println()
Expand Down
18 changes: 9 additions & 9 deletions julia/src/priorityqueue.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ struct PriorityQueue
end


@inline function is_prior(q::PriorityQueue, i::UInt32, j::UInt32)::Bool
@inline function is_prior(q::PriorityQueue, i::UInt32, j::UInt32)::Bool
@inbounds local a = q.tree[i]
@inbounds local b = q.tree[j]
a.first < b.first || (a.first == b.first && a.second < b.second)
end

@inline function swap!(q::PriorityQueue, i::UInt32, j::UInt32)
@inline function swap!(q::PriorityQueue, i::UInt32, j::UInt32)
@inbounds q.tree[i], q.tree[j] = q.tree[j], q.tree[i]
end


function empty(q::PriorityQueue)::Bool
function empty(q::PriorityQueue)::Bool
return length(q.tree) == 0
end

Expand All @@ -34,16 +34,16 @@ function push!(q::PriorityQueue, v::Visit)
push!(q.tree, v)
while index > 1
parentIndex::UInt32 = index >> 1
if is_prior(q, index, parentIndex)
if is_prior(q, index, parentIndex)
swap!(q, index, parentIndex)
index = parentIndex
else
else
break
end
end
end

function pop!(q::PriorityQueue)::Visit
function pop!(q::PriorityQueue)::Visit
result = q.tree[1]
top = pop!(q.tree)
size = length(q.tree)
Expand All @@ -55,14 +55,14 @@ function pop!(q::PriorityQueue)::Visit
index::UInt32 = 1
while true
child::UInt32 = (index << 1)
if child > size
if child > size
break
end
rIndex::UInt32 = child + 1
if rIndex <= size && is_prior(q, rIndex, child)
if rIndex <= size && is_prior(q, rIndex, child)
child = rIndex
end
if is_prior(q, index, child)
if is_prior(q, index, child)
break
end
swap!(q, index, child)
Expand Down
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function bench {
local langs="$1"
for count in 0 20; do
hyperfine --warmup 1 -L lang "$langs" 'cd {lang}; ./bench.sh '${count}' < ../data/Tokyo_Edgelist.csv' --export-json out/result-${count}.json
python plot_whisker.py --savefile out/result-${count}.png out/result-${count}.json
python plot_whisker.py --savefile out/result-${count}.png out/result-${count}.json
done
}

Expand Down

0 comments on commit e8b0c94

Please sign in to comment.