Skip to content
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
13 changes: 7 additions & 6 deletions bench/algorithm/fasta/1.v
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ fn main() {
n = strconv.atoi(os.args[1]) or { 1 }
}

mut iobuf := []u8 { len: 32*1024}
C.setvbuf(C.stdout, iobuf.data, C._IOFBF, iobuf.len)

mut rand := RandGen{
seed: 42
}
Expand Down Expand Up @@ -58,10 +61,8 @@ fn make_repeat_fasta(id string, desc string, src []byte, n int) {
println('>${id} ${desc}')
mut char_print_idx := 0
mut sb := strings.new_builder(line_width)
unsafe {
defer {
sb.free()
}
defer {
unsafe { sb.free() }
}
for _ in 0 .. (n / src.len + 1) {
for c in src {
Expand Down Expand Up @@ -92,8 +93,8 @@ fn make_random_fasta(mut rand_gen RandGen, id string, desc string, mut table map

mut n_char_printed := 0
mut sb := strings.new_builder(line_width)
unsafe {
defer {
defer {
unsafe {
sb.free()
}
}
Expand Down
132 changes: 132 additions & 0 deletions bench/algorithm/lru/3.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
module main

import os
import strconv

const a = u32(1103515245)
const c = u32(12345)
const m = u32(1) << 31


struct LCG {
mut:
seed u32
}

fn (mut lcg LCG) next() u32 {
lcg.seed = (a * lcg.seed + c) % m
return lcg.seed
}


struct Pair[K,V] {
mut:
k K
v V
}

struct ListNode[K,V] {
mut:
data Pair[K,V]
next &ListNode[K,V] = unsafe{nil}
prev &ListNode[K,V] = unsafe{nil}
}

struct LinkedList[K,V] {
mut:
head &ListNode[K,V] = unsafe { nil }
tail &ListNode[K,V] = unsafe { nil }
len int
}

struct LRU[K,V] {
size int
mut:
keys map[K] &ListNode[K,V]
entries LinkedList[K,V]
}


fn (mut list LinkedList[K,V]) add(data Pair[K,V]) &ListNode[K,V] {
mut node := &ListNode[K,V]{ data: data }
list.add_node(mut *node)
list.len += 1
return node
}


fn (mut list LinkedList[K,V]) add_node(mut node ListNode[K,V]) {
mut tail := list.tail
if list.head == unsafe { nil } { list.head = node }
else if tail != unsafe { nil } {
node.prev = tail
tail.next = node
}
list.tail = node
node.next = unsafe{ nil }
}


fn (mut list LinkedList[K,V]) remove(mut node ListNode[K,V]) {
if list.head == node { list.head = node.next }
if list.tail == node { list.tail = node.prev }
if node.prev != unsafe { nil } { node.prev.next = node.next }
if node.next != unsafe { nil } { node.next.prev = node.prev }
}


fn (mut list LinkedList[K,V]) move_to_end(mut node ListNode[K,V]) {
list.remove(mut *node)
list.add_node(mut *node)
}


fn (mut lru LRU[K, V]) get(key K) &V {
mut node := lru.keys[key] or { return unsafe{ nil } }
lru.entries.move_to_end(mut node)
return &node.data.v
}


fn (mut lru LRU[K, V]) put(key K, value V) {
mut node := lru.keys[key] or {
if lru.entries.len == lru.size {
mut head := lru.entries.head
lru.keys.delete(head.data.k)
head.data.k = key; head.data.v = value
lru.keys[key] = head
lru.entries.move_to_end(mut head)
return
}
lru.keys[key] = lru.entries.add(Pair[K, V] { k: key, v: value })
return
}
node.data.v = value
lru.entries.move_to_end(mut node)
}


fn main() {
mut size := 100
if os.args.len > 1 { size = strconv.atoi(os.args[1]) or { size } }

mut n := 100
if os.args.len > 2 { n = strconv.atoi(os.args[2]) or { n } }
mod := u32(size) * 10

mut rng0 := LCG{ seed: 0 }
mut rng1 := LCG{ seed: 1 }
mut lru := LRU[u32, u32] { size: size, keys : {}, entries: LinkedList[u32, u32]{} }

mut hit := 0
mut missed := 0

for _ in 0 .. n {
n0 := rng0.next() % mod
lru.put(n0, n0)
n1 := rng1.next() % mod
if lru.get(n1) != unsafe{ nil } { hit += 1 }
else { missed += 1 }
}
println('${hit}\n${missed}')
}
4 changes: 2 additions & 2 deletions bench/algorithm/pidigits/2.v
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ fn main() {
mut w := zero

mut sb := strings.new_builder(12 + n.str().len)
unsafe {
defer {
defer {
unsafe {
sb.free()
}
}
Expand Down