Skip to content

Commit 7f12fa7

Browse files
committed
feat: optimized interpreted variant to be more able to give information about the time difference
1 parent 04e6ea2 commit 7f12fa7

1 file changed

Lines changed: 58 additions & 27 deletions

File tree

taurus_poc/src/main.rs

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,94 @@
1-
use std::fs::{read_to_string};
1+
use std::collections::HashMap;
2+
use std::fs::read_to_string;
23
use std::io::BufRead;
34
use std::time::Instant;
45

56
macro_rules! add_integers {
67
($first:expr, $second:expr) => {
7-
$first+$second
8+
$first + $second
89
};
910
}
1011

1112
macro_rules! subtract_integers {
1213
($first:expr, $second:expr) => {
13-
$first-$second
14+
$first - $second
1415
};
1516
}
1617

1718
macro_rules! multiply_integers {
1819
($first:expr, $second:expr) => {
19-
$first*$second
20+
$first * $second
2021
};
2122
}
2223

24+
type OperationFn = fn(i32, i32) -> i32;
25+
26+
fn add(a: i32, b: i32) -> i32 {
27+
add_integers!(a, b)
28+
}
29+
fn subtract(a: i32, b: i32) -> i32 {
30+
subtract_integers!(a, b)
31+
}
32+
fn multiply(a: i32, b: i32) -> i32 {
33+
multiply_integers!(a, b)
34+
}
35+
2336
/*
24-
Interpreted POC 1.
25-
This is just interprets incoming flows
26-
What can be optimised?
27-
- flow till be typed so that no string processing is needed
28-
- compromise match statement into map
29-
- import macros from shared macro module
30-
31-
Av. execution time: 627.2µs
32-
*/
37+
Interpreted POC 1.
38+
This is just interprets incoming flows
39+
What can be optimised?
40+
- flow till be typed so that no string processing is needed
41+
- compromise match statement into map
42+
- import macros from shared macro module
43+
44+
Av. execution time: 800-600µs
45+
46+
Interpreted POC 2.
47+
Moved Timer after string processing to consider typed request that are incoming
48+
Function call will be put into HashMap to simulate function registry
49+
50+
Av. execution time: 190-160µs
51+
*/
3352
fn main() {
34-
let start = Instant::now();
53+
// Simulate Standard Function Registry
54+
let mut operations: HashMap<&str, OperationFn> = HashMap::new();
55+
operations.insert("add", add);
56+
operations.insert("sub", subtract);
57+
operations.insert("multiply", multiply);
3558

36-
for line in read_to_string("taurus_poc/poc.txt").unwrap().lines() {
59+
// Simulate typed gRPC Messages.
60+
// Will be slower in reality but at least no string processing is needed
61+
let mut functions: Vec<(&str, i32, i32)> = Vec::new();
3762

63+
let binding = read_to_string("taurus_poc/poc.txt").unwrap();
64+
for line in binding.lines() {
3865
let parts: Vec<&str> = line.split('|').collect();
3966

4067
if parts.len() == 3 {
4168
let operator = parts[0];
4269
let first: i32 = parts[1].parse().unwrap_or(0);
4370
let second: i32 = parts[2].parse().unwrap_or(0);
4471

45-
let result = match operator {
46-
"add" => add_integers!(first, second),
47-
"sub" => subtract_integers!(first, second),
48-
"multiply" => multiply_integers!(first, second),
49-
_ => {
50-
println!("Unsupported operator: {}", operator);
51-
continue;
52-
}
53-
};
54-
55-
println!("{} {} {} = {}", operator, first, second, result);
72+
functions.push((operator, first, second));
5673
} else {
5774
println!("Invalid line format");
5875
}
5976
}
6077

78+
// Here would start the main application
79+
let start = Instant::now();
80+
81+
// Resolve incoming requests
82+
for func in functions {
83+
match operations.get(func.0) {
84+
Some(operation) => {
85+
let result = operation(func.1, func.2);
86+
println!("{} {} {} = {}", &func.0, func.1, func.2, result);
87+
}
88+
None => println!("Unsupported operator: {}", func.0),
89+
}
90+
}
91+
6192
let duration = start.elapsed();
6293
println!("Time elapsed in expensive_function() is: {:?}", duration);
63-
}
94+
}

0 commit comments

Comments
 (0)