Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Results when compiled with -prod, on Ubuntu 20.04, with latest V 0.4.11 7e35d40, on i3-3225,
- // cc is the stock gcc-10 (`cc (Ubuntu 10.5.0-1ubuntu1~20.04) 10.5.0`):
- /*
- ```
- #0 11:57:02 ^ master ~/v>alias xtime='/usr/bin/time -f "CPU: %Us\tReal: %es\tElapsed: %E\tRAM: %MKB\t%C"'
- #0 11:57:08 ^ master ~/v>v -prod /home/delian/code/misc/2025_07_03__08/xx_v_iterator_atof_quick.v
- #0 11:57:23 ^ master ~/v>xtime /home/delian/code/misc/2025_07_03__08/xx_v_iterator_atof_quick 10
- 1th speed: 123632950.29 l/s
- 2th speed: 123553007.41 l/s
- 3th speed: 123801582.11 l/s
- 4th speed: 121633929.99 l/s
- 5th speed: 120313187.74 l/s
- 6th speed: 121775120.81 l/s
- 7th speed: 122016272.04 l/s
- 8th speed: 121878149.10 l/s
- 9th speed: 119665317.17 l/s
- 10th speed: 123450181.73 l/s
- average speed: 122171969.84 lines/s
- CPU: 2.44s Real: 2.57s Elapsed: 0:02.57 RAM: 402980KB /home/delian/code/misc/2025_07_03__08/xx_v_iterator_atof_quick 10
- #0 11:57:44 ^ master ~/v>
- ```
- */
- module main
- import os
- import strconv
- import time
- import rand
- const count_lines = 10_000_000
- fn generate_fake_code() string {
- mut b := []u8{cap: count_lines * 10}
- for _ in 0 .. count_lines {
- b << rand.f32().str().bytes()
- b << `\n`
- }
- return b.bytestr()
- }
- fn parse(code string) u32 {
- mut total := f64(0)
- _ = total
- mut count := u32(0)
- for line in new_line_iter(code) {
- if line.len == 0 {
- continue
- }
- total += strconv.atof_quick(line) // gcc manages to eliminate this call completely, when -prod (i.e. -O3) is used
- count++
- }
- return count
- }
- fn main() {
- mut arr := []f64{}
- times := u32(os.args[1].f32())
- code := generate_fake_code()
- for _ in 0 .. times {
- start := time.now().unix_nano()
- lines := parse(code)
- close := time.now().unix_nano()
- seconds := f64(close - start) / 1e9
- speed := f64(lines) / seconds
- arr << speed
- }
- mut total := f64(0)
- for i, val in arr {
- total += val
- println('${i + 1}th speed: ${val:.2f} l/s')
- }
- avg := total / f64(arr.len)
- println('average speed: ${avg:.2f} lines/s')
- }
- struct LineIter {
- text string
- mut:
- ptr &u8
- end &u8
- }
- fn new_line_iter(text string) LineIter {
- return unsafe {
- LineIter{
- text: text
- ptr: text.str
- end: text.str + text.len
- }
- }
- }
- @[direct_array_access]
- fn (mut it LineIter) next() ?string {
- unsafe {
- if it.ptr >= it.end {
- return none
- }
- start := it.ptr
- for it.ptr < it.end && *it.ptr != `\n` {
- it.ptr++
- }
- line_len := it.ptr - start
- line := tos(start, int(line_len))
- if it.ptr < it.end && *it.ptr == `\n` {
- it.ptr++
- }
- return line
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement