Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save genkuroki/e1066a2e77a7326ff7c0da35227a1079 to your computer and use it in GitHub Desktop.
Save genkuroki/e1066a2e77a7326ff7c0da35227a1079 to your computer and use it in GitHub Desktop.
write, read, compile by gcc, and run
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "ccode = raw\"\"\"\n#include <stdio.h>\nint main() {\n printf(\"Hello, World\\n\");\n return 0;\n}\n\"\"\"\nwrite(\"helloworld.c\", ccode)",
"execution_count": 1,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 1,
"data": {
"text/plain": "76"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "run(`ls -l helloworld.c`);",
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": "-rw-r--r-- 1 genkuroki genkuroki 76 May 22 09:42 helloworld.c\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "str = read(\"helloworld.c\", String)",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "\"#include <stdio.h>\\nint main() {\\n printf(\\\"Hello, World\\\\n\\\");\\n return 0;\\n}\\n\""
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "str = read(\"helloworld.c\", String) |> print",
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": "#include <stdio.h>\nint main() {\n printf(\"Hello, World\\n\");\n return 0;\n}\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "display(\"text/markdown\", \"```C\\n\"* read(\"helloworld.c\", String) * \"\\n```\")",
"execution_count": 5,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/markdown": "```C\n#include <stdio.h>\nint main() {\n printf(\"Hello, World\\n\");\n return 0;\n}\n\n```"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "[\n `gcc -O3 helloworld.c -o helloworld.exe`\n `ls -l helloworld.exe`\n `./helloworld`\n] .|> run",
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": "-rwxr-xr-x 1 genkuroki genkuroki 293391 May 22 09:42 helloworld.exe\nHello, World\r\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": "3-element Vector{Base.Process}:\n Process(`\u001b[4mgcc\u001b[24m \u001b[4m-O3\u001b[24m \u001b[4mhelloworld.c\u001b[24m \u001b[4m-o\u001b[24m \u001b[4mhelloworld.exe\u001b[24m`, ProcessExited(0))\n Process(`\u001b[4mls\u001b[24m \u001b[4m-l\u001b[24m \u001b[4mhelloworld.exe\u001b[24m`, ProcessExited(0))\n Process(`\u001b[4m./helloworld\u001b[24m`, ProcessExited(0))"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "VERSION",
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 7,
"data": {
"text/plain": "v\"1.7.0-DEV.1129\""
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "using Libdl, BenchmarkTools\n\npimcmc_c = \"\"\"\n#include <stdlib.h>\n#include <time.h>\n\ndouble pimcmc(long n_iter) {\n srand((unsigned)time(NULL));\n int n_in = 0;\n for (long i = 1; i <= n_iter; i++) {\n double x = (double)rand() / RAND_MAX;\n double y = (double)rand() / RAND_MAX;\n if (x * x + y * y < (double) 1.0)\n n_in++;\n }\n return ((double) 4.0)*((double) n_in)/((double) n_iter);\n}\n\"\"\"\n\ndisplay(\"text/markdown\", \"```C\\n\" * pimcmc_c * \"\\n```\")\n\nlibpimcmc = tempname()\nlibpimcmc_dl = libpimcmc * \".\" * Libdl.dlext\nopen(`gcc -Wall -O3 -march=native -xc -shared -o $libpimcmc_dl -`, \"w\") do f\n print(f, pimcmc_c)\nend\npimcmc_gcc(n::Int64) = @ccall libpimcmc.pimcmc(n::Int64)::Float64\n\n@show pimcmc_gcc(10^6)\nprintln()\n\nprint(\"gcc version:\")\n@btime pimcmc_gcc(10^6);",
"execution_count": 8,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/markdown": "```C\n#include <stdlib.h>\n#include <time.h>\n\ndouble pimcmc(long n_iter) {\n srand((unsigned)time(NULL));\n int n_in = 0;\n for (long i = 1; i <= n_iter; i++) {\n double x = (double)rand() / RAND_MAX;\n double y = (double)rand() / RAND_MAX;\n if (x * x + y * y < (double) 1.0)\n n_in++;\n }\n return ((double) 4.0)*((double) n_in)/((double) n_iter);\n}\n\n```"
},
"metadata": {}
},
{
"output_type": "stream",
"text": "pimcmc_gcc(10 ^ 6) = 3.139824\n\ngcc version: 22.898 ms (0 allocations: 0 bytes)\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "using Random\n\nfunction pimcmc(n)\n rng = Random.default_rng()\n n_in = 0\n for i in 1:n\n x = rand(rng)\n y = rand(rng)\n n_in += x^2 + y^2 ≤ 1\n end\n 4n_in/n\nend\n\n@show pimcmc(10^6)\nprintln()\n\nprint(\"julia version:\")\n@btime pimcmc(10^6);",
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"text": "pimcmc(10 ^ 6) = 3.140888\n\njulia version: 2.046 ms (0 allocations: 0 bytes)\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "julia-1.7-depwarn-o3",
"display_name": "Julia 1.7.0-DEV depwarn -O3",
"language": "julia"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"base_numbering": 1,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "1.7.0"
},
"@webio": {
"lastKernelId": null,
"lastCommId": null
},
"gist": {
"id": "e1066a2e77a7326ff7c0da35227a1079",
"data": {
"description": "write, read, compile by gcc, and run",
"public": true
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/e1066a2e77a7326ff7c0da35227a1079"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment