Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's an example of how you can make a fast BF interpreter:</p> <pre><code>int main(int argc, char **argv) { if( !(argc &gt; 1) ) return 1; unsigned char *progmem = argv[1]; unsigned char *cellmem = malloc(sizeof(char)*CELLSPACE); if(cellmem == NULL) return 1; unsigned char **loopdepth = malloc(sizeof(char*)*MAXLOOPDEPTH); if(loopdepth == NULL) return 1; unsigned char *origcellmem = cellmem; unsigned char **origloopdepth = loopdepth; for(;;) { switch(*progmem) { case '+': ++*cellmem; break; case '-': --*cellmem; break; case '&gt;': cellmem++; break; case '&lt;': cellmem--; break; case '[': *loopdepth = progmem-1; loopdepth++; break; case ']': loopdepth--; if(*cellmem) { progmem = *loopdepth; } break; case '.': putc(*cellmem, stdout); break; case ',': *cellmem = getc(stdin); break; case '\0': free(origcellmem); free(origloopdepth); return 0; } progmem++; } } </code></pre> <p>Alright so, the highlights of my code that should make it faster than your solution:</p> <p>I'm not doing any checking each loop, the compiler is likely to generate a raw unconditional loop here (or so the C wizards tell me.) And since I'm using the raw data from the string instead of structs I just have to put '\0' at the end of the switch! This means the only time my interpreter checks if it needs to end the program is when nothing else matches the switch.</p> <p>I'm using simple pointers for everything, and only manipulating those, I'm not doing arithmatic on integers and then accessing the pointed to memory with the [] operators, I'm simply manipulating the pointers and their pointed to memory directly.</p> <p>I'm using a simple 'stack' to hold loops, this limits the maximum depth of loops you can have but 32 is plenty enough for most brainfuck programs, and it's adjustable in source.</p> <p>I've ordered the switch case for how often things appear, seeing as + and - are the most common brainfuck characters, and then > and &lt; and then [ and ] and finally the input characters. I am not 100% on this but I'm -pretty sure- the order of the switch matters IF ONLY A LITTLE BIT!</p> <p>I'm not doing any error checking, I'm assuming the user's input is perfect. While this might not give nice errors, like running out of bounds and such, that's EXACTLy what languages do at runtime, a C program can generate a segfault easily, we probably -shouldn't- check for these. (A quick note, mine generated a LOT of segfaults in writing this :P)</p> <p>And lastly one possible optimization I thought of:</p> <p>Run length encoding, like used in compression. You could run length encode the brainfuck in a simple format, so that: +++ turns into 3+ and the interpreter 'gets' it and instead of adding one three times, it adds three one time. The performance gain here could be AMAZING in some places</p> <p>And there you go, that's really all I've got. I don't know C amazingly well so I might have made some mistakes, but I tried my best, feel free to benchmark the provided code, I've got no idea exactly how fast it runs. It accepts input as a command line argument.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload