Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "time"
- "fmt"
- "strings"
- "strconv"
- )
- func pop(stack *[]int) int {
- last := len(*stack) - 1
- ret := (*stack)[last]
- *stack = (*stack)[:last]
- return ret
- }
- func main() {
- input := `10 20 100 + - 20 * DUP 10 20 + * =`;
- t := time.Now()
- for i := 0; i<10000; i++ {
- stack := []int{}
- for _, chunk := range strings.Split(input, " ") {
- switch (chunk) {
- case "+":
- stack = append(stack, pop(&stack) + pop(&stack))
- case "-":
- stack = append(stack, pop(&stack) - pop(&stack))
- case "*":
- stack = append(stack, pop(&stack) * pop(&stack))
- case "=":
- stack = append(stack, (map[bool]int{true: 1, false: 0})[pop(&stack) == pop(&stack)])
- case "DUP":
- stack = append(stack, stack[len(stack)-1])
- case "DUMP":
- fmt.Println(stack)
- default:
- val, _ := strconv.Atoi(chunk)
- stack = append(stack, val)
- }
- }
- }
- fmt.Printf("Go stack: %s\n", time.Since(t))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement