12 ms·
Python consumes 38x more energy than Java
- _han 3y agoI would like to point out that the data in the table is from 2017, and CPython recently focused a lot on performance.
- boesboes 3y agoThe actual benchmark: https://benchmarksgame-team.pages.debian.net/benchmarksgame/performance/spectralnorm.html https://benchmarksgame-team.pages.debian.net/benchmarksgame/... EDIT: fwiw, classic Fortran is twice as fast as java :P As is Rust, but that's less funny to me
- zelphirkalt 3y agoInteresting, what is "Chapel"? Will need to look that up. EDIT: seems to be https://chapel-lang.org/ https://chapel-lang.org/
- yread 3y agoIt is very fast and very memory efficient apparently
- thumbuddy 3y agoFortran is a beast. People crap on it all the time yet use it often millions of times a day without realizing it.
- chii 3y ago> People crap on it all the time only clueless people looking for clout online!
- skissane 3y ago> Fortran is a beast. People crap on it all the time I think most of the people who "crap on" Fortran, when they say "Fortran", mean FORTRAN 77, not Fortran 2018
- smcl 3y agoI don't think Fortran gets beaten up that much, it's just sort of ... ignored
- Xenoamorphous 3y agoThere are only two kinds of languages...
- ilyt 3y agouse in what ? I severely doubt anything I do touches fortran millions times a day unless it's google search that runs on it.
- is_true 3y agoYour weather report
- ilyt 3y agoI don't use that million times a day. Maybe once a week
- dylanowen 3y agoThe banking system.
- colejohnson66 3y agoI thought banks used COBOL?
- dylanowen 3y agoI bet you're right and that I'm misremembering. I learned about it in the context of fronting mainframes with graphql so I wasn't thinking too much about the backend language.
- skissane 3y ago> I thought banks used COBOL? Banks have all kinds of random legacy crap written in all kinds of random languages. While COBOL is a lot more common, I guarantee you there are plenty of banks with bits of Fortran in their code bases. It is particularly found in older code for economic modelling, etc Back in the old days, a lot of apps were written in Fortran that would never be written in that today. I used to work for a university where the application used to determine whether a student had met the graduation requirements for their degree was written in Fortran. Why Fortran? It was a manual process, then one of the professors offered to automate it for them, and he wrote the app in Fortran, because that was the language he was most comfortable with. And 30 years later they were still running it (although they finally replaced it with a COTS package when I worked there) I once worked with an insurance company for whom a key business application was written in Turbo Pascal for DOS. They wrote it back in the 1980s when everyone had DOS machines. By the 2010s they were still running it in a VM. For all I know they are still doing that today
- xioxox 3y agoAs a Fortran user, it's pretty awful for string handling. It's fine for numerics. However, there's a lack of libraries for non-numeric stuff (e.g. data structures). The free compilers are buggy, too.
- hmaarrfk 3y agoThanks for this link. They also seemed to have avoided libraries like numpy. In my mind, python made it possible to hardware optimize with libraries like numpy quite easily. Avoiding it is a mistake. I'll try to see if I have time to play the game myself and throw my attempt in there.
- davidgrenier 3y agoBut isn't what makes numpy efficient written in C?
- phoe-krk 3y agoEverything that makes Python efficient is written in C or C++ or the like. Python in these situations is just a glue language (with an optional interactive layer) that makes using these libraries more feasible.
- btschaegg 3y agoWell, the Java source uses threads. Guess how that's implemented. FWIW if python provides an abstraction that keeps the code readable while keeping the efficiencies of C, I think that should count for python, not against it.
- kaba0 3y agoI mean.. threads are implemented by the OS, I don’t really see the equivalency here.
- KingOfCoders 3y agoIf your web service runs with numpy, excellent.
- prirun 3y agoA web service is not typically compute-bound, so it's doubtful a Python web app is going to use 38x more energy than a Java web app.
- igouy 3y agoNo, this is "the actual benchmark" — "Source: Energy Efficiency across Programming Languages, SLE’17" and google finds — https://greenlab.di.uminho.pt/wp-content/uploads/2017/09/paperSLE.pdf https://greenlab.di.uminho.pt/wp-content/uploads/2017/09/pap...
- Uninen 3y agoEven more clickbaity title would have been "Python consumes 70x more energy than Rust"
- ndlan 3y agoclickbaity and correct
- igouy 3y agoThe link is marketing for some consulting firm.
- ChrisRR 3y agoEverything consumes more energy than C
- ttoinou 3y agoExcept ASM, AVX etc.
- moomin 3y agoAnd, see above, Fortran.
- SomeRndName11 3y agono, VHDL and Verilog consume less
- thumbuddy 3y agoSometimes I wonder if that's true long term. The amount of bugs, security issues, and code churn surrounding C code bases is pretty wild. If everybody and all their deployments has to recompile a C code base 100 times over the course of three years to get patches, fixes, etc it makes me wonder what the real cost of C is? My intuition states that this isn't the case, but intuition can be wrong.
- 3y ago
- zelphirkalt 3y agoWhile Java's compiler or JIT is probably one of the most optimized softwares in existence, how do we measure, what overhead the language nudges the programmers to build? I am thinking of AbstractProviderFactoryProxy and similar. Or that for a very long time it forced you (or still does to a degree) to put everything into classes, breeding 1 or 2 generations of programmers, who see a noun and jump to making a class and then subsequently initializing the class to be able to call a method, which actually would only need to be a standalone function. It probably does not balance the 38x, and probably the JIT optimizes parts of it away, but surely the cost is far from zero. Also what about the computational resources it takes to just run a JVM, compared to running a Python program?
- DarkNova6 3y agoPlease explain how "AbstractProviderFactoryProxy" is related to the language. I have taken over as lead for a Python project and what I see all the Java problems people used to make fun of (none have worked in Java before) and worse. On top, everything has an interface starting with "I", C# style. Methods in entities, service classes are instantiated with state etc. And side-effects are all over the place. If you are writing modern Java syntax, there is little that is more stable and readable than good modern Java syntax.
- zelphirkalt 3y ago> Please explain how "AbstractProviderFactoryProxy" is related to the language. The language Java did, until some time ago, not allow you to pass functions as arguments. You had to make an anonymous inner class for example, satisfying some interface. Many design patterns are very much oriented towards a language like Java, which does/did not allow for higher order functions. Take the visitor pattern for example. It will get you a "Visitor" in your class name implementing a visitor. How would it work in other languages, which provide higher order functions or always did so? Well, you would simply write a function and depending on whether your language has static types, annotate its types for arguments and return value. You would then pass this function in as an argument, whose name can be "visitor". Take a factory pattern as an example. How would it work? Well, a factory can be expressed using a function, that returns another function. It takes the arguments, that specify/narrow down how the returned function works. In Java it would become a Factory class instead. Instead of using a simple function, one needs (needed?) to build a whole class around that, because one could not return a function. So one would build that class and then make it return an object instead, which of course again must be derived from some class ... Just 2 examples that quickly come to mind. > I have taken over as lead for a Python project and what I see all the Java problems people used to make fun of (none have worked in Java before) and worse. It is certainly true, that people also write shit code in Python. I have seen very popular libraries, which wrap REST APIs in objects. It is silly, because objects are meant to have some lifetime in which they communicate with each other and possibly change their state. There is no changing state though. Everything is a response from the REST API, which by the definition of REST should transfer the representative state in its responses. I don't want any in between stored state. I want the state that the API gives me. It is very much a more functional view on things. But Python programmers will go "make classes!" nevertheless. Because noun. Because not thinking about whether they really need a class. Because thinking that one approach fits it all and the only approaches they learned were procedural at the beginning of their learning and then OOP, which is complex enough to take years to learn properly, so that is where they stopped. However, Python always (for a very long time?) has allowed you to pass procedures as arguments and as such does not encourage "AbstractProviderFactoryProxy" as much as Java does. Still, sometimes former Java developers try their hand at some Python code ... > If you are writing modern Java syntax, there is little that is more stable and readable than good modern Java syntax. Modern Java certainly has improved. But there is a lot of relearning to be done for those generations of Java programmers, to get rid of the "every noun a class" mentality.
- awelxtr 3y agoDoes this table take into account energy used while developing/maintaining?
- thumbuddy 3y agoSincerely doubt it does because that's a difficult thing to measure. If you are writing a lambda function at a FAANG company you do way more than break even with a language that has a heavy compilation time. If you're fixing a comment for a hobby project that doesn't ship binaries that you and your nephew use, definitely not. The deployment and consumption of said code matters a lot for that.
- speedgoose 3y agoNo. Here is the source of the table: https://greenlab.di.uminho.pt/wp-content/uploads/2017/10/sleFinal.pdf https://greenlab.di.uminho.pt/wp-content/uploads/2017/10/sle...
- marginalia_nu 3y agoOne should always be skeptical of these sorts of benchmarks, especially against Java, and I say this as primarily a Java developer. Well optimized Java is very lean and can be incredibly performant, but the median modern Java app is an obese cronenberg made out of gigabytes of SpringBoot dependencies. That's not particularly fast; and well optimized python will run circles around it.
- boesboes 3y agoThis is an important point, real life performance looks nothing like benchmarks in most cases. From my personal experience: benchmarks showed Jruby was MUCH MUCH faster the 'normal' Cruby. But try running a basic rails application on jruby and it's 10-100x slower, even after minutes of repeating the same request. (disclaimer, this was a few years back and purely anecdotal)
- renegade-otter 3y ago"Artificial tests produce artificial results".
- ramblerman 3y agoYou seem to be conflating speed and memory usage. A spring boot app will generally be very performant. It will indeed use more memory though.
- estebank 3y agoI did an excercise some time ago and managed to get two file transfer apps, one in Rust and one in Java, where the performance was effectively the same (in some cases with a slight Java edge!), but the memory consumption was orders of magnitude in favor of Rust. The JVM is a wonder of engineering, and Java is indeed quite fast.
- vkazanov 3y agoMost reasonable people in the industry understand that Java can be fast, especially in benchmark-oriented code. The problem is that the OOP-first ideology of the language and the coding culture surrounding it is not performance-oriented, especially with modern machines that don't like pointer-chasing. One not very well known fact is that just-in-time compilers have more optimisation-specific info than pure ahead-of-time compilers, i.e. most used code paths, real types used for polymorphic values, etc. That is, until your AOT compiler uses something like a profile-guided optimisation. In practice though writing to performance in Java takes a very dedicated effort. Real Java programs are horrible with memory, okay (for an AOT languguage) in long-running compute-intensive tasks, and painfully slow at short-living tasks such as CLI utils. Almost any real program written in C would run circles around most off-the-shelf Java programs. Turns out, though, all of that doesn't really matter :-)
- radicalbyte 3y agoThis is why I'm learning Rust: C# gives fast development speeds than Python with decent performance. Python and Go for when their native libraries smoke C# and now Rust for performance or low level.
- kaba0 3y agoGo is not generally faster than C#/Java. There are scenarios where each can come up on top.
- radicalbyte 3y agoSmoke as in have better support for specific (niche) use cases. Python has strong numeric libraries and Go has better support for modern/interesting/crazy cryptography systems than C#/Java. So in reality there are very few places where you would want to use Go over modern C#.
- deleted 3y ago[deleted]
- boredumb 3y agoJust write in Rust, it's easier to build good software than either of these languages (yea, yea, do you data analyst stuff in python until you start using POLARS). At this point it's a far superior ecosystem from a developer experience point of view and the fact it's going to get you as close to efficient as possible without you thinking too far into it is a welcome side effect. Using VScode with the Sqlx package I get compile time errors on my SQL - as a result I haven't ran a binary with a typo from my SQL in the last 12 months.
- TachyonicBytes 3y agoDo you use an extension other than just `rust-analyzer` in order to get compile-time errors for SQL with sqlx?
- movpasd 3y agoAre there any good interactive programming options in Rust? That's the main draw of a dynamic language like Python for ad hoc data science tasks, the semantics just lines up well with the highly iterative nature of the work.
- boredumb 3y agoI know I saw earlier in the year a Jupyter notebook for rust - but I haven't used it personally so I'm not sure how nicely it will play. I use jupyter+python to view and play with the data usually and then write my actual job in rust with Polars to use as the ETL or whatever i'm doing with it at that moment.
- fancyfredbot 3y agoC++ uses 34% more energy than C? I thought C code was generally also valid C++ code and so you really should be able to get the same performance and energy consumption. Very odd.
- tasubotadas 3y agoYou can write C in C++ but it won't be idiomatic C++. I've seen plenty of experienced "C++" developers that have no idea what is "shared_ptr".
- dahart 3y agoTrue partly because shared_ptr is a relatively new language feature; I’ve been locked to a ~decade-old C++ compiler for most of my professional career, and so has everyone I know, just because support for a current compiler across platforms at any given time has been historically so spotty. It seems to be improving at the moment. > it won’t be idiomatic I just looked at the first example: C: https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/spectralnorm-gcc-3.html https://benchmarksgame-team.pages.debian.net/benchmarksgame/... C++: https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/spectralnorm-gpp-8.html https://benchmarksgame-team.pages.debian.net/benchmarksgame/... The C++ doesn’t use shared_ptr or STL or anything, and isn’t that far from the C code, but it isn’t that close either. I wonder if it’s OMP configuration that’s causing it to be slower, or maybe just instruction cache or something since the C++ is larger. The results say this one has C++ going 50% slower. I’m a little skeptical it’s the compiler’s or the language’s fault. I’d speculate it might have more to do with how the program is written.
- dahart 3y agoOh it does too use STL, guess I’m blind. That could be part of the reason it’s slower.
- igouy 3y agoYou seem to be looking at the wrong source code. The "Energy Efficiency across Programming Languages, SLE’17"authors provided this repo — https://github.com/greensoftwarelab/Energy-Languages/blob/master/C/spectral-norm/spectralnorm.gcc-4.c https://github.com/greensoftwarelab/Energy-Languages/blob/ma... https://github.com/greensoftwarelab/Energy-Languages/tree/master/C%2B%2B/spectral-norm https://github.com/greensoftwarelab/Energy-Languages/tree/ma... For a single outlier (regex-redux) there's a 12x difference between the measured times of the selected C and C++ programs. Even so, that mostly messes up the results because the arithmetic mean is used rather than the median. https://benchmarksgame-team.pages.debian.net/benchmarksgame/sometimes-people-just-make-up-stuff.html#averages https://benchmarksgame-team.pages.debian.net/benchmarksgame/...
- mg 3y agoIs Python's "values don't change, they get replaced by new ones stored elsewhere" approach what makes it slow? I would like to see some micro-benchmarks which pin down the bottlenecks. For example, this minimal benchmark with one variable and a few control structures: x=0 for i in range(int(10e6)): if i<50: x=x+1 print (x) Runs 5 times slower here than the same in PHP: $x=0; for ($i=0; $i<10e6; $i++) if ($i<50) $x++; echo "$x\n"; Tested with: time python3 loop.py Which gives me 0.31s And: time php loop.php Which gives me 0.06s A factor of five seems to be roughly the average when comparing Python to PHP for a bunch of different code constructs I tried.
- snicker7 3y agoYou can’t use time command in this sort of benchmark. You are including start up t times here.
- mg 3y agoStartup times seem to be the same and also negligible: time python3 nothing.py 0.011s time php nothing.php 0.011s
- d-k-bo 3y agoStartup time in Python depends on the program because Python compiles all modules to bytecode before executing them.
- mg 3y agoWhen I up the loop size by a factor of 10, Python takes 10x longer. So I don't think the compile time plays a role here. Similar for the PHP version. python3 loop.py 3.450s time php loop.php 0.469s So PHP is 7x faster for the longer loop.
- igouy 3y agoPHP JIT ?
- misja111 3y agoAmazing. I have seen this list popping up in my LinkedIn feed for more than a year already and it keeps coming back. While any benchmark is of course interesting when considered on its own, the conclusions that people draw from this list tend to be complete nonsense: "Python is bad for the environment", "we should switch to language xxx to combat global warming" etc. First of all, especially for the slower languages in this list, it is extremely rare that application code written in that language is the bottleneck of a performance critical application. Typically the bottlenecks of every day applications tend to be databases and network. If you need heavy number crunching, there tend to be excellent libraries available written in lower level languages, such as e.g. for Python NumPy and Pandas. If you are really concerned about the energy footprint of your application, you're probably better off with optimizing these components. Or optimizing the higher level architecture of your own application, which tends to be a lot easier in a higher level langguage. Second, coding in a faster language is often not economically possible. Programming the same application in C will normally take much longer than coding it in Python. Developing time costs money and can also mean loss of opportunity. And finally, the 'greenest' language of them all was not even included in this list: assembly. I guess the author must have realized in the back of his head that such a difficult language wasn't a realistic option for switching to a more energy efficient solution. He/she just failed to realize that this argument applies to many other languages as well.
- mempko 3y agoI think you bring up a good point. Our economic system isn't designed to reward low energy consumption. In fact, GDP and energy usage are very tightly linked. And because we are seeking growth in GDP at a system level, we are doomed to use more energy.
- screamingninja 3y ago> GDP and energy usage are very tightly linked Correlation or causation?
- slowmovintarget 3y ago
- number6 3y agoSo Python is 38x more powerful?!
- pella 3y agoThe research: "Energy Efficiency across Programming Languages" SLE’17, October 23–24, 2017 PDF: https://repositorio.inesctec.pt/bitstream/123456789/5492/1/P-00N-4CX.pdf https://repositorio.inesctec.pt/bitstream/123456789/5492/1/P... The human mental energy required for programming, unfortunately, has not been measured. ;-)
- bigfryo 3y agoSo python programmers are destroying the planet.. I just knew it all along
- Rochus 3y agoThere is a more recent paper by Pereira et al. from 2021 (see https://www.sciencedirect.com/science/article/abs/pii/S0167642321000022 https://www.sciencedirect.com/science/article/abs/pii/S01676... ); if geomean is used (which is the correct method) instead of what they apparently do in the paper, there is yet a different order (see http://software.rochus-keller.ch/Ranking_programming_languages_by_energy_efficiency_evaluation.ods http://software.rochus-keller.ch/Ranking_programming_languag...).
- igouy 3y agoods ? "How do you want to open this file?"
- Rochus 3y agoIt's OpenOffice format; you should be able to open it with MS Excel too in case you prefer that.
- igouy 3y agoDidn't open with ancient Excel. HTML? txt?
- Rochus 3y agoMust be really ancient. Here is table 4 by geomean: (c) C 1.0 (c) Rust 1.1 (c) C++ 1.1 (c) Swift 1.3 (c) Fortran 1.7 (v) Java 1.7 (c) Ada 2.1 (c) Chapel 2.3 (c) Ocaml 2.4 (v) C# 3.0 (c) Go 3.4 (c) Pascal 5.1 (v) F# 5.2 (c) Haskell 5.3 (v) Lisp 6.1 (i) Dart 7.8 (i) JavaScript 8.2 (v) Racket 8.5 (i) Hack 11.0 (v) Erlang 14.6 (i) TypeScript 22.7 (i) PHP 28.7 (i) Jruby 31.6 (i) Ruby 42.6 (i) Python 51.9 (i) Perl 60.5 (i) Lua 78.9 And here the Rosetta algorithms (table 3) by geomean: (c) C 1.0 (c) Rust 3.8 (c) Fortran 4.1 (v) Lisp 4.3 (c) Pascal 4.7 (c) Go 6.5 (c) C++ 7.3 (c) OCaml 7.9 (v) Java 16.2 (i) JavaScript 16.2 (c) Ada 17.3 (i) Dart 24.4 (c) Haskell 26.3 (c) Chapel 51.4 (i) Ruby 56.8 (v) Racket 59.7 (i) Lua 89.7 (i) PHP 91.5 (v) Erlang 108.7 (i) Python 115.9 (i) Perl 166.6
- jjaken 3y agoNow compare mental energy
- tmaly 3y agoI can see C having 1.00 in table 4 in the article. But how does Python get 71.90 and JavaScript 4.45 ? Something seems off there.
- igouy 3y ago71.90 arithmetic average more than the 10 selected C programs. 6.52 arithmetic average more than the 10 selected C programs. Seems off because?
- kaba0 3y agoJS has a JIT compiler, so for long running programs it can very well execute native machine code in the exact same way as C (plus a GC occasionally meddling with things). Also, it’s not even a bad JIT compiler, huge amount of development went into making it good. Standard Python is interpreted all the time, going from instruction to instruction giving a layer of abstraction that never disappears.
- tmaly 3y agoI agree a JIT works well for long running programs, but is that the situation we have here with these tests?