Zomis / Brainduck

Brainfuck Interpreter in Java/Groovy, with a Groovy DSL
40 stars 1 forks source link

Code Analysis and optimization #4

Closed Zomis closed 8 years ago

Zomis commented 9 years ago

First of all, remember that the only thing that can change a Brainfuck program behavior is user-input, with the , Brainfuck command.

Optimize some things for speed (number of instructions being performed when running), optimize some things for code compactness.

Zomis commented 9 years ago

See https://github.com/Zomis/BrainDuck/commit/b5ee5ecaee01a41697b408821e8ba89aa1cc151c and a couple of parent commits for optimizations and the reasoning behind them

Zomis commented 9 years ago

Also see https://github.com/Zomis/BrainDuck/commit/3e84dcb1da27b8380e6ba343f4a9cbdd9fc7b429 and http://codegolf.stackexchange.com/questions/16732/minify-brainfuck

Zomis commented 9 years ago

About >>-<+> which can become simply >+>-:

Reset the check after each [ ] . ,, when one of those characters come up, analyse the recent > < + - characters and see if the effect caused by those characters can be achieved in a better way.

For example, >>-<+> results in the integer array [ 0, 1, -1 ] and the offset 2 (meaning that we have moved 2 steps to the right after the sequence has been performed). The shortest way to accomplish the same integer array is clearly to do >+>-.

The reason to not include [ ] in the check is because that is a conditional check, and needs a loop-analyse before any conclusions can be drawn about whether or not it will always be performed a specific number of times or not. Possibly include those in the analyse at a later time.