{"id":18780444,"url":"https://github.com/karan/libfib","last_synced_at":"2025-04-13T11:30:54.677Z","repository":{"id":65984356,"uuid":"60440698","full_name":"karan/libfib","owner":"karan","description":"An experiment to run Go code directly from Python.","archived":false,"fork":false,"pushed_at":"2016-06-05T03:10:46.000Z","size":5,"stargazers_count":29,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-04T23:29:49.724Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/karan.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":"2016-06-05T03:04:21.000Z","updated_at":"2023-09-08T17:11:10.000Z","dependencies_parsed_at":"2023-02-19T18:15:20.034Z","dependency_job_id":null,"html_url":"https://github.com/karan/libfib","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karan%2Flibfib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karan%2Flibfib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karan%2Flibfib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karan%2Flibfib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/karan","download_url":"https://codeload.github.com/karan/libfib/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248705517,"owners_count":21148549,"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":[],"created_at":"2024-11-07T20:26:24.559Z","updated_at":"2025-04-13T11:30:54.408Z","avatar_url":"https://github.com/karan.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# libfib\n\nAn experiment to run Go code directly from Python.\n\n### How it works\n\n`libfib.go` is a very naive implementation of Fibonacci sequence. Using Go's build modes, we build it into a shared library. The library is imported in Python and used there.\n\nBasically, we execute the Go code from Python code even though the latter doesn't even know what Go is.\n\nRun `go help buildmodes` to learn more about Go's build modes.\n\n### Why?\n\n1. It's cool.\n2. Go is much faster than Python. You can rewrite slow parts of your code in Go, and call them from your Python code. See [benchmarks](#benchmarks).\n3. It's cool.\n\n### Benchmarks\n\nBenchmarks are the best way to compare the performance and really show the **coolness** of Go's build modes.\n\n*The function to benchmark:* Find the first 40 Fibonacci numbers. That is `Fib` function will be called with `n` from `1...40`. The execution time of the full loop is then measured.\n\n*Results*: `libfib` is 76X faster!!!1111\n\n#### Pure Python implementation\n\n`benchmark.py` implements the same Fibonacci function in pure Python.Here's the result of the Linux `time` command:\n\n```\n$ time python benchmark.py\n\nreal  2m26.726s\nuser  2m6.232s\nsys 0m2.280s\n```\n\n#### Shared library implementation\n\n`benchmark_libfib.py` calls the Fibonacci function in the shared library from Python. Here's the result of the Linux `time` command:\n\n```\n$ time python benchmark_libfib.py\n\nreal  0m1.899s\nuser  0m1.593s\nsys 0m0.039s\n```\n\n### Creating the shared objects file\n\n```\ngo build -buildmode=c-shared -o libfib.so libfib.go\n```\n\nThis will produce `fib.h` and `fib.so`.\n\n### Using `.so` in Python\n\nNow we can load the library in Python and start using it.\n\n```\nimport ctypes\nlib = ctypes.CDLL(\"libfib.so\")\nprint(lib.Fib(20))\n```\n\n### License\n\nMIT (c) Karan Goel\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaran%2Flibfib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkaran%2Flibfib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaran%2Flibfib/lists"}