Skip to content

Commit

Permalink
13x speedup!!!
Browse files Browse the repository at this point in the history
14.331s -> 1.112s calculating the factorial of 100,000 on my machine
  • Loading branch information
pgattic committed Jun 1, 2024
1 parent ff26102 commit a1bdbe9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
8 changes: 2 additions & 6 deletions fact.crn
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@

$fact # Factorial
$fact # This function pops 1 and puts 1 overall
dup -- dup ?++:fact *

$say-hello
"Hello, " print print "!" println

$main
"Jacob" say-hello
12 8 * putln "Hello World!" println
100000 fact putln

10 changes: 5 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ fn execute(code: HashMap<&str, Vec<&str>>) {
if !code.contains_key("main") { // I can has main function?
return;
}
let mut instructions: Vec<&str> = code["main"].iter().cloned().collect();
let mut instructions: Vec<&str> = code["main"].clone();
instructions.reverse();
let mut last_func: &str = "main";

while !instructions.is_empty() {
// println!("{:?} {:?}", stack, instructions);
let c_instr = instructions[0];
instructions.remove(0);
let c_instr = instructions.pop().unwrap();
match c_instr.parse::<BigInt>() {
Ok(val) => {
stack.push(val);
Expand Down Expand Up @@ -269,7 +269,7 @@ fn execute(code: HashMap<&str, Vec<&str>>) {
}
};
if !options[index].is_empty() {
instructions.insert(0, options[index]);
instructions.push(options[index]);
}
}
}
Expand All @@ -282,7 +282,7 @@ fn execute(code: HashMap<&str, Vec<&str>>) {
if let Some(values) = code.get(c_instr) {
last_func = c_instr;
for value in values.iter().rev() {
instructions.insert(0, *value);
instructions.push(*value);
}
} else {
eprintln!("ERROR: Unresolved Symbol: \"{}\"", c_instr);
Expand Down

0 comments on commit a1bdbe9

Please sign in to comment.