{"id":13437728,"url":"https://github.com/exaloop/codon","last_synced_at":"2025-05-13T20:03:09.062Z","repository":{"id":64457309,"uuid":"410989014","full_name":"exaloop/codon","owner":"exaloop","description":"A high-performance, zero-overhead, extensible Python compiler with built-in NumPy support","archived":false,"fork":false,"pushed_at":"2025-05-06T18:31:15.000Z","size":6158,"stargazers_count":15645,"open_issues_count":95,"forks_count":535,"subscribers_count":139,"default_branch":"develop","last_synced_at":"2025-05-06T19:51:58.183Z","etag":null,"topics":["compiler","gpu-programming","high-performance","llvm","numpy","parallel-programming","python"],"latest_commit_sha":null,"homepage":"https://docs.exaloop.io/codon","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/exaloop.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2021-09-27T17:56:34.000Z","updated_at":"2025-05-06T14:10:56.000Z","dependencies_parsed_at":"2024-01-23T23:29:23.687Z","dependency_job_id":"d12a2eb2-57b9-404b-84aa-95d851402572","html_url":"https://github.com/exaloop/codon","commit_stats":{"total_commits":426,"total_committers":15,"mean_commits":28.4,"dds":"0.45070422535211263","last_synced_commit":"37ff25a907e8eecf8223f999533ed2aa9e0cee0c"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/exaloop%2Fcodon","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/exaloop%2Fcodon/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/exaloop%2Fcodon/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/exaloop%2Fcodon/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/exaloop","download_url":"https://codeload.github.com/exaloop/codon/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254020472,"owners_count":22000749,"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":["compiler","gpu-programming","high-performance","llvm","numpy","parallel-programming","python"],"created_at":"2024-07-31T03:00:59.755Z","updated_at":"2025-05-13T20:03:09.042Z","avatar_url":"https://github.com/exaloop.png","language":"Python","funding_links":[],"categories":["Python","C++","Software","Python 程序","Basic Components","Starchart","语言资源库","Python / C++","Compilers","Programming","Python Compilers"],"sub_categories":["Trends","网络服务_其他","Python Performance Booster","c++","Mojo🔥FastAPI Client","Python"],"readme":"\u003ch1 align=\"center\"\u003e\n \u003cimg src=\"docs/img/codon-banner.svg\" alt=\"Codon banner\"/\u003e\n\u003c/h1\u003e\n\n\u003ch3 align=\"center\"\u003e\n  \u003ca href=\"https://docs.exaloop.io/codon\" target=\"_blank\"\u003e\u003cb\u003eDocs\u003c/b\u003e\u003c/a\u003e\n  \u0026nbsp;\u0026#183;\u0026nbsp;\n  \u003ca href=\"https://docs.exaloop.io/codon/general/faq\" target=\"_blank\"\u003e\u003cb\u003eFAQ\u003c/b\u003e\u003c/a\u003e\n  \u0026nbsp;\u0026#183;\u0026nbsp;\n  \u003ca href=\"https://exaloop.io/blog\" target=\"_blank\"\u003e\u003cb\u003eBlog\u003c/b\u003e\u003c/a\u003e\n  \u0026nbsp;\u0026#183;\u0026nbsp;\n  \u003ca href=\"https://join.slack.com/t/exaloop/shared_invite/zt-1jusa4kc0-T3rRWrrHDk_iZ1dMS8s0JQ\" target=\"_blank\"\u003eChat\u003c/a\u003e\n  \u0026nbsp;\u0026#183;\u0026nbsp;\n  \u003ca href=\"https://docs.exaloop.io/codon/general/roadmap\" target=\"_blank\"\u003eRoadmap\u003c/a\u003e\n  \u0026nbsp;\u0026#183;\u0026nbsp;\n  \u003ca href=\"https://exaloop.io/#benchmarks\" target=\"_blank\"\u003eBenchmarks\u003c/a\u003e\n\u003c/h3\u003e\n\n\u003ca href=\"https://github.com/exaloop/codon/actions/workflows/ci.yml\"\u003e\n  \u003cimg src=\"https://github.com/exaloop/codon/actions/workflows/ci.yml/badge.svg\"\n       alt=\"Build Status\"\u003e\n\u003c/a\u003e\n\n# What is Codon?\n\nCodon is a high-performance Python implementation that compiles to native machine code without\nany runtime overhead. Typical speedups over vanilla Python are on the order of 10-100x or more, on\na single thread. Codon's performance is typically on par with (and sometimes better than) that of\nC/C++. Unlike Python, Codon supports native multithreading, which can lead to speedups many times\nhigher still.\n\n*Think of Codon as Python reimagined for static, ahead-of-time compilation, built from the ground\nup with best possible performance in mind.*\n\n## Goals\n\n- :bulb: **No learning curve:** Be as close to CPython as possible in terms of syntax, semantics and libraries\n- :rocket: **Top-notch performance:** At *least* on par with low-level languages like C, C++ or Rust\n- :computer: **Hardware support:** Full, seamless support for multicore programming, multithreading (no GIL!), GPU and more\n- :chart_with_upwards_trend: **Optimizations:** Comprehensive optimization framework that can target high-level Python constructs\n  and libraries\n- :battery: **Interoperability:** Full interoperability with Python's ecosystem of packages and libraries\n\n## Non-goals\n\n- :x: *Drop-in replacement for CPython:* Codon is not a drop-in replacement for CPython. There are some\n  aspects of Python that are not suitable for static compilation — we don't support these in Codon.\n  There are ways to use Codon in larger Python codebases via its [JIT decorator](https://docs.exaloop.io/codon/interoperability/decorator)\n  or [Python extension backend](https://docs.exaloop.io/codon/interoperability/pyext). Codon also supports\n  calling any Python module via its [Python interoperability](https://docs.exaloop.io/codon/interoperability/python).\n  See also [*\"Differences with Python\"*](https://docs.exaloop.io/codon/general/differences) in the docs.\n\n- :x: *New syntax and language constructs:* We try to avoid adding new syntax, keywords or other language\n  features as much as possible. While Codon does add some new syntax in a couple places (e.g. to express\n  parallelism), we try to make it as familiar and intuitive as possible.\n\n## How it works\n\n\u003cp align=\"center\"\u003e\n \u003cimg src=\"docs/img/codon-figure.svg\" width=\"90%\" alt=\"Codon figure\"/\u003e\n\u003c/p\u003e\n\n# Quick start\n\nDownload and install Codon with this command:\n\n```bash\n/bin/bash -c \"$(curl -fsSL https://exaloop.io/install.sh)\"\n```\n\nAfter following the prompts, the `codon` command will be available to use. For example:\n\n- To run a program: `codon run file.py`\n- To run a program with optimizations enabled: `codon run -release file.py`\n- To compile to an executable: `codon build -release file.py`\n- To generate LLVM IR: `codon build -release -llvm file.py`\n\nMany more options are available and described in [the docs](https://docs.exaloop.io/codon/general/intro).\n\nAlternatively, you can [build from source](https://docs.exaloop.io/codon/advanced/build).\n\n# Examples\n\n## Basics\n\nCodon supports much of Python, and many Python programs will work with few if any modifications.\nHere's a simple script `fib.py` that computes the 40th Fibonacci number...\n\n``` python\nfrom time import time\n\ndef fib(n):\n    return n if n \u003c 2 else fib(n - 1) + fib(n - 2)\n\nt0 = time()\nans = fib(40)\nt1 = time()\nprint(f'Computed fib(40) = {ans} in {t1 - t0} seconds.')\n```\n\n... run through Python and Codon:\n\n```\n$ python3 fib.py\nComputed fib(40) = 102334155 in 17.979357957839966 seconds.\n$ codon run -release fib.py\nComputed fib(40) = 102334155 in 0.275645 seconds.\n```\n\n## Using Python libraries\n\nYou can import and use any Python package from Codon via `from python import`. For example:\n\n```python\nfrom python import matplotlib.pyplot as plt\ndata = [x**2 for x in range(10)]\nplt.plot(data)\nplt.show()\n```\n\n(Just remember to set the `CODON_PYTHON` environment variable to the CPython shared library,\nas explained in the [the Python interoperability docs](https://docs.exaloop.io/codon/interoperability/python).)\n\n## Parallelism\n\nCodon supports native multithreading via [OpenMP](https://www.openmp.org/). The `@par` annotation\nin the code below tells the compiler to parallelize the following `for`-loop, in this case using\na dynamic schedule, chunk size of 100, and 16 threads.\n\n```python\nfrom sys import argv\n\ndef is_prime(n):\n    factors = 0\n    for i in range(2, n):\n        if n % i == 0:\n            factors += 1\n    return factors == 0\n\nlimit = int(argv[1])\ntotal = 0\n\n@par(schedule='dynamic', chunk_size=100, num_threads=16)\nfor i in range(2, limit):\n    if is_prime(i):\n        total += 1\n\nprint(total)\n```\n\nNote that Codon automatically turns the `total += 1` statement in the loop body into an atomic\nreduction to avoid race conditions. Learn more in the [multithreading docs](https://docs.exaloop.io/codon/advanced/parallel).\n\nCodon also supports writing and executing GPU kernels. Here's an example that computes the\n[Mandelbrot set](https://en.wikipedia.org/wiki/Mandelbrot_set):\n\n```python\nimport gpu\n\nMAX    = 1000  # maximum Mandelbrot iterations\nN      = 4096  # width and height of image\npixels = [0 for _ in range(N * N)]\n\ndef scale(x, a, b):\n    return a + (x/N)*(b - a)\n\n@gpu.kernel\ndef mandelbrot(pixels):\n    idx = (gpu.block.x * gpu.block.dim.x) + gpu.thread.x\n    i, j = divmod(idx, N)\n    c = complex(scale(j, -2.00, 0.47), scale(i, -1.12, 1.12))\n    z = 0j\n    iteration = 0\n\n    while abs(z) \u003c= 2 and iteration \u003c MAX:\n        z = z**2 + c\n        iteration += 1\n\n    pixels[idx] = int(255 * iteration/MAX)\n\nmandelbrot(pixels, grid=(N*N)//1024, block=1024)\n```\n\nGPU programming can also be done using the `@par` syntax with `@par(gpu=True)`. See the\n[GPU programming docs](https://docs.exaloop.io/codon/advanced/gpu) for more details.\n\n## NumPy support\n\nCodon includes a feature-complete, fully-compiled native NumPy implementation. It uses the same\nAPI as NumPy, but re-implements everything in Codon itself, allowing for a range of optimizations\nand performance improvements.\n\nHere's an example NumPy program that approximates $\\pi$ using random numbers...\n\n``` python\nimport time\nimport numpy as np\n\nrng = np.random.default_rng(seed=0)\nx = rng.random(500_000_000)\ny = rng.random(500_000_000)\n\nt0 = time.time()\n# pi ~= 4 x (fraction of points in circle)\npi = ((x-1)**2 + (y-1)**2 \u003c 1).sum() * (4 / len(x))\nt1 = time.time()\n\nprint(f'Computed pi~={pi:.4f} in {t1 - t0:.2f} sec')\n```\n\n... run through Python and Codon:\n\n```\n$ python3 pi.py\nComputed pi~=3.1417 in 2.25 sec\n$ codon run -release pi.py\nComputed pi~=3.1417 in 0.43 sec\n```\n\nCodon can speed up NumPy code through general-purpose and NumPy-specific compiler optimizations,\nincluding inlining, fusion, memory allocation elision and more. Furthermore, Codon's NumPy\nimplementation works with its multithreading and GPU capabilities, and can even integrate with\n[PyTorch](https://pytorch.org). Learn more in the [Codon-NumPy docs](https://docs.exaloop.io/codon/interoperability/numpy).\n\n# Documentation\n\nPlease see [docs.exaloop.io](https://docs.exaloop.io) for in-depth documentation.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fexaloop%2Fcodon","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fexaloop%2Fcodon","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fexaloop%2Fcodon/lists"}