{"id":15581426,"url":"https://github.com/alexprengere/pythonbrainfuck","last_synced_at":"2025-09-08T13:35:30.358Z","repository":{"id":5807152,"uuid":"7021965","full_name":"alexprengere/PythonBrainFuck","owner":"alexprengere","description":"A very (!) fast BrainFuck interpreter in Python","archived":false,"fork":false,"pushed_at":"2022-10-24T15:50:29.000Z","size":1102,"stargazers_count":20,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-08T20:24:15.284Z","etag":null,"topics":["brainfuck-interpreter","jit","pypi","python","rpython"],"latest_commit_sha":null,"homepage":"http://alexprengere.github.io/PythonBrainFuck/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alexprengere.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-12-05T17:57:11.000Z","updated_at":"2025-03-24T23:15:47.000Z","dependencies_parsed_at":"2023-01-11T16:53:12.598Z","dependency_job_id":null,"html_url":"https://github.com/alexprengere/PythonBrainFuck","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/alexprengere/PythonBrainFuck","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexprengere%2FPythonBrainFuck","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexprengere%2FPythonBrainFuck/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexprengere%2FPythonBrainFuck/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexprengere%2FPythonBrainFuck/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexprengere","download_url":"https://codeload.github.com/alexprengere/PythonBrainFuck/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexprengere%2FPythonBrainFuck/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263742015,"owners_count":23504365,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["brainfuck-interpreter","jit","pypi","python","rpython"],"created_at":"2024-10-02T19:46:52.624Z","updated_at":"2025-07-05T12:32:44.191Z","avatar_url":"https://github.com/alexprengere.png","language":"Python","readme":"A very (!) fast BrainFuck interpreter in Python\n===============================================\n\nHere is a BrainFuck example:\n\n```bf\n+++++ +++++             initialize counter (cell #0) to 10\n[                       use loop to set the next four cells to 70/100/30/10\n\u003e +++++ ++              add  7 to cell #1\n\u003e +++++ +++++           add 10 to cell #2\n\u003e +++                   add  3 to cell #3\n\u003e +                     add  1 to cell #4\n\u003c\u003c\u003c\u003c -                  decrement counter (cell #0)\n]\n\u003e ++ .                  print 'H'\n\u003e + .                   print 'e'\n+++++ ++ .              print 'l'\n.                       print 'l'\n+++ .                   print 'o'\n\u003e ++ .                  print ' '\n\u003c\u003c +++++ +++++ +++++ .  print 'W'\n\u003e .                     print 'o'\n+++ .                   print 'r'\n----- - .               print 'l'\n----- --- .             print 'd'\n\u003e + .                   print '!'\n\u003e .                     print '\\n'\n```\n\nHow to use the interpreter:\n\n```bash\npython2 ./bf.py hello.bf\nHello World!\n```\n\n## Speeding things up\n\n### With Pypy\n\nIf you try to run a long BrainFuck program like `mandel.b`, you will realize our interpreter is pretty slow.\n\n```bash\npython2 ./bf.py examples/mandel.b\n# wait 1h45\n```\n\nA first simple way of speeding things up is to use Pypy instead of CPython.\n\n```bash\nPYPY_VERSION=\"pypy2.7-v7.3.9\"\nwget \"https://downloads.python.org/pypy/${PYPY_VERSION}-linux64.tar.bz2\"\ntar -xjf \"${PYPY_VERSION}-linux64.tar.bz2\"\nmv \"${PYPY_VERSION}-linux64\" pypy\n# Only 1m30 now!\n./pypy/bin/pypy ./bf.py ./examples/mandel.b\n```\n\n### With a JIT\n\nThe interpreter is actually written in RPython, so it can be statically compiled using the Pypy toolchain.\nDownload the latest source of Pypy and uncompress it in a `pypy-src` folder. Note that you could also install `rpython` from PyPI.\n\n```bash\nwget \"https://downloads.python.org/pypy/${PYPY_VERSION}-src.tar.bz2\"\ntar -xjf \"${PYPY_VERSION}-src.tar.bz2\"\nmv \"${PYPY_VERSION}-src\" pypy-src\n```\n\nThen you can build from the Python script `bf.py` an executable binary `bf-c`:\n\n```bash\n# The compilation will take about 20s\npython2 pypy-src/rpython/bin/rpython bf.py\n# Mandelbrot now completes in 32s\n./bf-c examples/mandel.b\n```\n\nYou can rebuild the `bf-c` using `--opt=jit` to add a JIT to your BrainFuck interpreter:\n\n```bash\n# The compilation will take about 7m (you can speed this up by using Pypy)\npython2 pypy-src/rpython/bin/rpython --opt=jit bf.py\n# Mandelbrot now completes in about 5 seconds(!)\n./bf-c examples/mandel.b\n```\n\n### Let's compare with a C implementation\n\nI also looked for a [fast BrainFuck interpreter](http://mazonka.com/brainf/), written in C. After compilation with `gcc -O3` (6.2), running `mandel.b` take about 5 seconds to run, so it is in the same order of magnitude as the JIT version (without `-O3`, it takes 10 seconds).\n\n```bash\ngcc -O3 ./resources/bff4.c -o bff4\n# About 5s\n./bff4 \u003c examples/mandel.b\n```\n\n### Let's compile the BrainFuck directly\n\nTo complete those numbers, I finally tested a [Brainfuck to C translator](https://gist.github.com/Ricket/939687), then compiled the C version of the `mandel.b` program. With `-O3`, the compiled `mandel.b` runs in a bit less than 1 second (without `-O3`, it takes 15 seconds).\n\n```bash\ngcc resources/brainfucc.c -o brainfucc\n./brainfucc \u003c examples/mandel.b \u003e mandel.c\ngcc -O3 mandel.c -o mandel\n# 950ms\n./mandel\n```\n\n### Summary\n\nHere is a summary of the speed gain I could observe on Ubuntu 16.10 (core i7, 8Go of RAM), running `mandel.b`:\n\n* the initial `bf.py` with CPython (2.7): about 1h45 (baseline)\n* the initial `bf.py` with Pypy (5.6.0): 1m30s (70x)\n* the `bf-c` without JIT: 32s (x200)\n* the `bf-c` with JIT: 5 seconds (x1250)\n* the `bff4` C implementation: 5 seconds with `-O3`, 10 seconds without\n* the `mandel` binary built when compiling `mandel.b` directly: 1 second with `-O3`, 15 seconds without\n\nThe JIT addition contains code from [this amazing tutorial on JITs](http://morepypy.blogspot.fr/2011/04/tutorial-part-2-adding-jit.html).\n\nIf the BrainFuck interpreter `bf.py`  is a bit hairy to look at, you can check out the [step_by_step](step_by_step) folder to go from the simplest interpreter, then a bit better, then\nusing only RPython code, then with the JIT-specific code, then with some final optimizations.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexprengere%2Fpythonbrainfuck","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexprengere%2Fpythonbrainfuck","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexprengere%2Fpythonbrainfuck/lists"}