{"id":27,"date":"2016-01-11T10:32:00","date_gmt":"2016-01-11T10:32:00","guid":{"rendered":"https:\/\/sandrock.co.za\/carl\/2016\/01\/11\/efficient-compiled-binaries-arent-running-c-code\/"},"modified":"2016-01-11T10:32:00","modified_gmt":"2016-01-11T10:32:00","slug":"efficient-compiled-binaries-arent-running-c-code","status":"publish","type":"post","link":"https:\/\/sandrock.co.za\/carl\/2016\/01\/efficient-compiled-binaries-arent-running-c-code\/","title":{"rendered":"Efficient compiled binaries aren&#8217;t &#8220;running C code&#8221;"},"content":{"rendered":"<p>I use Python for most of my programming needs, and since the CPython interpreter is not particularly efficient, it is relatively common for people to write the CPU-intensive bits of their code in other languages like C, C++ or (for numerical work) Fortran. Python allows for a great deal of flexibility in defining modules in other languages and using their compiled routines with little overhead.<\/p>\n<p>I have developed an issue about how people talk about this as though the languages themselves were in play at that inner level. People will often say: &#8220;Python is slow&#8221;, then when you point out that libraries like NumPy make many tasks quite fast, they will retort that NumPy calls out to C and Fortran extension code. Often they will express this as actually &#8220;running C code&#8221;. I take issue with this, because there isn&#8217;t much left of the original language when compiling to native machine instructions. Any toolchain which allows you to produce efficient binaries could have been used.<\/p>\n<p>This argument relates to a similar argument which has C being the base of all languages, since many interpreters and compilers are themselves written in C. People will sometimes scoff at people using other languages because those languages are effectively a thin wrapper around the &#8220;underlying&#8221; C code. This is wrong, because if you wrote a compiler for a new language in C and compiled it to produce a binary, the binary you are running which compiles your new language is an independent thing.<\/p>\n<p>Many languages are self-hosting, which means that their compilers are written in the languages themselves. This is not surprising when you consider that the first C compiler was probably developed through a bootstrapping process which may have included manual assembly of code to a rudimentary compiler, which was then used to compile subsequent versions. Once a good compiler exists, it is unnecessary to bootstrap this way. You&#8217;re probably better off just writing your compiler in a different language which already has a good compiler. In fact, you may be able to save time by writing a compiler which produces C code rather than native machine instructions, and then use a C compiler to compile that code. Self-hosting is a useful way to show the completeness of your language, but it is not required for your language to be an independent thing.<\/p>\n<p>It can be hard to reason about this Inception-like chain of compilers producing compilers, but luckily one doesn&#8217;t actually have to. Once you have a compiler which takes code written in a language and produces a runnable executable, the language actually doesn&#8217;t matter. In the case of Python, there is an interpreter running at runtime, so it is accurate so say that the particular interpreter is executing tasks slowly. But importantly, the language Python is different from a particular interpreter. I can run the same Python program on CPython, PyPy, IronPython or Jython and get different performance due to the details of how each interpreter interprets the code. Even these interpreters aren&#8217;t actually running the code as I wrote it line by line, but rather executing a bytecode version of the code which is the result of a compilation step. This distinction is meaningless in practice, as the mapping of Python code to bytecode is quite simple and relatively one-to-one, unlike the mapping from C code to machine instructions, where most compilers will apply optimisations such that the final instructions are often very different from the code you wrote.<\/p>\n<p>In summary, <i>languages<\/i> aren&#8217;t fast or slow \u2013 interpreters or compilers are more or less efficient. Back to the first point, it may be more accurate to describe the speed benefit obtained by using NumPy as being from using efficient compiled routines rather than &#8220;running C code&#8221;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I use Python for most of my programming needs, and since the CPython interpreter is not particularly efficient, it is relatively common for people to write the CPU-intensive bits of their code in other languages like C, C++ or (for numerical work) Fortran. Python allows for a great deal of flexibility in defining modules in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[1],"tags":[],"class_list":["post-27","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/posts\/27","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/comments?post=27"}],"version-history":[{"count":0,"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/posts\/27\/revisions"}],"wp:attachment":[{"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/media?parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/categories?post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/tags?post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}