Created
February 26, 2020 20:28
-
-
Save spytheman/cac3c6c18022fd90d666807b11f4ba91 to your computer and use it in GitHub Desktop.
Experiment with f32 vs f64 functions in V.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import math | |
| import benchmark | |
| const ( MAX_REPEATS = 32_000_000 ) | |
| type Float = f32 | f64 | |
| fn g_sqrt (num Float) Float { | |
| match num { | |
| f32 { return math.sqrtf(it) } | |
| f64 { return math.sqrt(it) } | |
| else { panic('Received argument of type ${typeof(num)}, expected f32 or f64') } | |
| } | |
| } | |
| fn main () { | |
| v32 := 123.0 | |
| v64 := f64(123.0) | |
| af32 := [v32].repeat(MAX_REPEATS) | |
| af64 := [v64].repeat(MAX_REPEATS) | |
| mut bmark := benchmark.start() | |
| for i in 0..MAX_REPEATS { math.sqrtf(v32) } bmark.measure('math.sqrtf(v32)') | |
| for i in 0..MAX_REPEATS { math.sqrt(v64) } bmark.measure('math.sqrt(v64)') | |
| for i in 0..MAX_REPEATS { math.sqrtf(123.0) } bmark.measure('math.sqrtf(123.0)') | |
| for i in 0..MAX_REPEATS { math.sqrt(f64(123.0)) } bmark.measure('math.sqrt(f64(123.0))') | |
| for i in 0..MAX_REPEATS { math.sqrtf(f32(i)) } bmark.measure('math.sqrtf(f32(i))') | |
| for i in 0..MAX_REPEATS { math.sqrt(f64(i)) } bmark.measure('math.sqrt(f64(i))') | |
| for i in 0..MAX_REPEATS { math.sqrtf(af32[i]) } bmark.measure('math.sqrtf(af32[i])') | |
| for i in 0..MAX_REPEATS { math.sqrt(af64[i]) } bmark.measure('math.sqrt(af64[i])') | |
| for i in 0..MAX_REPEATS { g_sqrt(f32(i)) } bmark.measure('g_sqrt(f32(i))') | |
| for i in 0..MAX_REPEATS { g_sqrt(f64(i)) } bmark.measure('g_sqrt(f64(i))') | |
| for i in 0..MAX_REPEATS { g_sqrt(af32[i]) } bmark.measure('g_sqrt(af32[i])') | |
| for i in 0..MAX_REPEATS { g_sqrt(af64[i]) } bmark.measure('g_sqrt(af64[i])') | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment