SHOW:
|
|
- or go back to the newest paste.
| 1 | // Results when compiled with -prod, on Ubuntu 20.04, with latest V 0.4.11 7e35d40, on i3-3225, | |
| 2 | // cc is the stock gcc-10 (`cc (Ubuntu 10.5.0-1ubuntu1~20.04) 10.5.0`): | |
| 3 | /* | |
| 4 | ``` | |
| 5 | #0 11:57:02 ^ master ~/v>alias xtime='/usr/bin/time -f "CPU: %Us\tReal: %es\tElapsed: %E\tRAM: %MKB\t%C"' | |
| 6 | #0 11:57:08 ^ master ~/v>v -prod /home/delian/code/misc/2025_07_03__08/xx_v_iterator_atof_quick.v | |
| 7 | #0 11:57:23 ^ master ~/v>xtime /home/delian/code/misc/2025_07_03__08/xx_v_iterator_atof_quick 10 | |
| 8 | 1th speed: 123632950.29 l/s | |
| 9 | 2th speed: 123553007.41 l/s | |
| 10 | 3th speed: 123801582.11 l/s | |
| 11 | 4th speed: 121633929.99 l/s | |
| 12 | 5th speed: 120313187.74 l/s | |
| 13 | 6th speed: 121775120.81 l/s | |
| 14 | 7th speed: 122016272.04 l/s | |
| 15 | 8th speed: 121878149.10 l/s | |
| 16 | 9th speed: 119665317.17 l/s | |
| 17 | 10th speed: 123450181.73 l/s | |
| 18 | average speed: 122171969.84 lines/s | |
| 19 | 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 | |
| 20 | #0 11:57:44 ^ master ~/v> | |
| 21 | ``` | |
| 22 | */ | |
| 23 | module main | |
| 24 | ||
| 25 | import os | |
| 26 | import strconv | |
| 27 | import time | |
| 28 | import rand | |
| 29 | ||
| 30 | const count_lines = 10_000_000 | |
| 31 | ||
| 32 | fn generate_fake_code() string {
| |
| 33 | mut b := []u8{cap: count_lines * 10}
| |
| 34 | for _ in 0 .. count_lines {
| |
| 35 | b << rand.f32().str().bytes() | |
| 36 | b << `\n` | |
| 37 | } | |
| 38 | return b.bytestr() | |
| 39 | } | |
| 40 | ||
| 41 | fn parse(code string) u32 {
| |
| 42 | mut total := f64(0) | |
| 43 | _ = total | |
| 44 | mut count := u32(0) | |
| 45 | for line in new_line_iter(code) {
| |
| 46 | if line.len == 0 {
| |
| 47 | continue | |
| 48 | } | |
| 49 | - | total += strconv.atof_quick(line) |
| 49 | + | total += strconv.atof_quick(line) // gcc manages to eliminate this call completely, when -prod (i.e. -O3) is used |
| 50 | count++ | |
| 51 | } | |
| 52 | return count | |
| 53 | } | |
| 54 | ||
| 55 | fn main() {
| |
| 56 | mut arr := []f64{}
| |
| 57 | times := u32(os.args[1].f32()) | |
| 58 | code := generate_fake_code() | |
| 59 | ||
| 60 | for _ in 0 .. times {
| |
| 61 | start := time.now().unix_nano() | |
| 62 | lines := parse(code) | |
| 63 | close := time.now().unix_nano() | |
| 64 | ||
| 65 | seconds := f64(close - start) / 1e9 | |
| 66 | speed := f64(lines) / seconds | |
| 67 | arr << speed | |
| 68 | } | |
| 69 | ||
| 70 | mut total := f64(0) | |
| 71 | for i, val in arr {
| |
| 72 | total += val | |
| 73 | println('${i + 1}th speed: ${val:.2f} l/s')
| |
| 74 | } | |
| 75 | avg := total / f64(arr.len) | |
| 76 | ||
| 77 | println('average speed: ${avg:.2f} lines/s')
| |
| 78 | } | |
| 79 | ||
| 80 | struct LineIter {
| |
| 81 | text string | |
| 82 | mut: | |
| 83 | ptr &u8 | |
| 84 | end &u8 | |
| 85 | } | |
| 86 | ||
| 87 | fn new_line_iter(text string) LineIter {
| |
| 88 | return unsafe {
| |
| 89 | LineIter{
| |
| 90 | text: text | |
| 91 | ptr: text.str | |
| 92 | end: text.str + text.len | |
| 93 | } | |
| 94 | } | |
| 95 | } | |
| 96 | ||
| 97 | @[direct_array_access] | |
| 98 | fn (mut it LineIter) next() ?string {
| |
| 99 | unsafe {
| |
| 100 | if it.ptr >= it.end {
| |
| 101 | return none | |
| 102 | } | |
| 103 | start := it.ptr | |
| 104 | for it.ptr < it.end && *it.ptr != `\n` {
| |
| 105 | it.ptr++ | |
| 106 | } | |
| 107 | line_len := it.ptr - start | |
| 108 | line := tos(start, int(line_len)) | |
| 109 | if it.ptr < it.end && *it.ptr == `\n` {
| |
| 110 | it.ptr++ | |
| 111 | } | |
| 112 | return line | |
| 113 | } | |
| 114 | } | |
| 115 |