简体   繁体   中英

PHP vs. Java are there energy consumption differences?

I heard a rumor, that Java consumes less energy than PHP and was wondering if and how this would be true. I'm currently working in a company where we base most of our apps on PHP. Power consumption has never been the problem for us, but we are working on bigger projects where it might matter. We love PHP for web developing and are wondering how such a rumor can spread and if it is true at all.

The example I heard was that Facebook is switching for exactly that reason to Java (I can't seem to find any of this stuff on google though).

Since a customer of mine is asking me this question I would love proof if it is true.

Computers don't particularly care if they're executing Java or PHP. Power consumption is pretty much the same. The question then becomes a question of performance - if you can serve more requests with one server you'll need less servers and consume less power. Or alternatively, if you're not doing web scale applications, serve your requests quicker and spend more time idling, which consumes less power.

Given pure Java and pure PHP, Java as a statically typed JIT'ed language is of course faster. The question is rather which one can you make faster given the team members and development effort available to you.

My take is that the best way is to mix languages, use existing Java based infrastructural tools, such as Terracotta to build the performance critical parts and something more nimble to build complex but not that heavy business and presentation logic.

I really really doubt it is a language only issue.

The platforms in question have so much variability to render any generic comparison moot. To name a few variability points.

  • Servlet container for Java (Tomcat, Glassfish, Websphere, Jetty, ...)
  • Web server for PHP (Apache, IIS, lighttpd, nginx, ...)
  • Opcode caches for PHP
  • Libraries and framewokrs used
  • Operating systems
  • Hard disks involved
  • Cooling
  • Algorithms on the application itself

I really doubt you can isolate so many variables into a useful metric. At most you can pick two equivalent applications (noting all the platform's choices) using the same hardware and compare them. Then improve the worst until it tops the better one. The proper measurement would be both the watts per hour and requests per second, I think.

What's noted in Ants' answer (upvote him) is the crucial point though: the better performing platform will always be more power efficient, given enough demand , because it'll be able to serve the same amount of requests with less hardware.

But which platform is better performing is not merely language dependent, depends on the things noted above (and some more).

I was surprised by this question until I did a Google search, which turned this up. It's a serious issue, one that I wouldn't have thought of.

Now that I am thinking about it, I think it becomes an issue of who pays the electric bill. Sun's Java strategy was about selling servers: big iron for the back, thin clients for the front end.

Perhaps technologies like Flex move more of the work back to the client and leave them with a greater percentage of the electric bill.

But I'd be surprised to see a ranking of languages by energy use.

A very interesting question. I'm voting it up.

What a fascinating problem. Wouldn't it be interesting to write an application in a number of languages, deploy them on identical hardware, and measure the power consumption? I'd love to see it.

If you really get crazy, what about a performance monitoring tool that, in addition to showing you where memory and CPU were consumed in each part of your app, would also show you where the most energy was being used?

Now I wish I could vote this question up again.

Like many comparative questions here, you'll probably need to come up with a benchmark to really determine whether that's true.

lesswatts.org has a bit of information on applications power management , as well as several other aspects of power consumption on Linux systems. As a side note, they seem to be using PHP, so that might be worth something in itself :)

They keep repeating that you should use PowerTOP to determine which applications are causing the most power consumption, and you can see from the screenshot that they are checking wakeups from idle, at least.


Most of the time, a web server is sitting idle, then it "serves" for a very brief moment, then it goes back to waiting for the next connection to serve. In that regard, PHP would contribute very little to the entire process: only the serving portion. So I wonder if the actual benchmark was a comparison of a particular Java-based web server vs. Apache/PHP serving similar pages. If that was the case, it's not really a fair comparison of PHP and Java -- either of the two considered as serving an actual page is only active for milliseconds at a time, typically. I would think the actual web server, the one who's selecting or polling connections, is the one that would be hogging power.

By energy consumption do you mean Watts power consumption?

I'm not 100% sure, but even if this is true, I think this is similar to optimizing a part of your code, which is executed in 0.01% of your program's runtime.

The problems that will be caused by the switching (changing production/release platforms, learning curve time loss, new business software costs, etc) will be pretty drastic. I can't see such an important decision being made, except after serious and company-specific business analysis task and the corresponding results of it.

However, this should make for an interesting discussion.

I understand that many large companies have difficulty with growth in demands for resouces in their data centres. These include both floor space and power consumption. So I can well believe that rationalisation of applciations, favouring low resource consumption is a strategy that is being adopted.

Whether the use of Java, PHP or any other implementation technology is likely to be the determinant of power consumption is less obvious to me.

If we implemented a particular piece of function optimising for resource consumption in, say, Interpreted Basic, Java and C, which would we expect to need most execution resources.

I would need to see some hard evidence, but I could believe that a pure interpreted language might consume more than, Java, and even with JIT etc. it in turn might consume more than C.

Now just suppose (purely hypothetically, I'm not saying this is the case) that it was more effort to develop in C than in Java, and more effort to develop in Java than in Basic. How Would you trade off development and maintenance effort against that resource consumption?

Really tricky to do. If someone told me they were moving to Java soleley on the basis of power consumption I'd really want to know more. What are the costs in doing so? Where was the break-even point? [BTW - I work almost exclusively in Java, I'd love to be able to say "we win, see how low power we are!"]

This is a difficult question to answer unless, as was mentioned about benchmarks, you set some rules for comparison.

For example, comparing a Java3D first-person shooter to a PHP webpage would be unfair, as Java would lose.

If you look at Java frameworks, then you may want to compare these three: Tomcat + JDK 6 + JSP Apache + PHP Scala + Lift framework

I included Scala as it compiles to java bytecode and I expect it will be cheapest for power.

I am not certain what would be the winner, I would bet on Scala, but you would want to ensure that you have the same application implemented and then just compare the power usage.

PHP will probably win as apache and PHP seems to be lower on memory usage than Java, but I can't really compare Scala and PHP.

The big unknown for me is that if you call the same java code then it has already been turned into native code and so will run more quickly, but JSP should be precompiled to take advantage of Java.

But, if you use Web2.0 technologies then it changes, as you put most of the load on the browser, if you have a large javascript application, then you are just making server calls, which will reduce power usage on the server as the render work is passed onto the browser. At that point the JIT for Java should come into play, and Java or Scala I would expect to be lower on power usage.

A big test, IMO, would be to see if we can get the same performance as we reduce the size of the machine, so, if you need 3 computers for PHP or Java, that are load balanced, and 1 Scala machine can have the same performance, then scala (using Lift) will win.

Power efficiency is an entire field in itself. Measuring performance is usually the best way to go (given the same hardware). If different hardware is being compared, then that's a whole 'nother ball game.

So given the same hardware, if a software stack can perform better than another software stack, then it means that the better performing software stack will use less power "per request" than the other. If the performance difference is great enough for you to consolidate your servers into less, then it's a even bigger win!

There's a lot of other considerations:

Data Centers Consider that servers are housed in data centers that are cooled. Servers generate heat and the heat needs to be removed to protect the hardware. A/C units do not have infinite granularity. Their efficiency usually comes with volume. So if I reduce my number of servers from 2 to 1, I may have saved the power consumption of one server, but probably not much in cooling costs. But if changing my architecture allows me to cut out 100 servers... that's big savings!

Hardware and Peripheral Devices Using the most power efficient SW stack and running it on a Pentium 4 server = stupidity ;) The most energy efficient software cannot make up for inefficient hardware. One interesting lesson here is: "let the hardware guys worry about power". You worry about getting your application to market. When your application can generate revenue, you can always buy the latest 16-core Core 5 hexa-deca CPU and instantly get your energy efficiency ;)

Virtualizaton If your application is low volume, consolidating it into a virtual machine running on multi-core system would probably save you more energy than rewriting it in your most energy efficient SW stack and running it on standalone server.

Programmer Time You need programmers knowledgeable in the "most power efficient" software stack. You must consider if that is the right tools. Programmers use computers to develop the software and the more time they need to develop (if they're constrained to the wrong tools), the more power is consumed. Not to mention you will have to pay them for more hours. This usually overrides any energy consumption concerns because the costs here are magnitudes higher.

If your only concern is energy efficiency, yes, use all the tools in the bag to get you there. But most of the time, that is only one small variable in the overall scheme of things, and also the least of your cost.

More efficient code does consume less resources, including power. Java in general has a faster, more efficient implementation, so all other factors being constant, Java will probably consume less energy running on a server.

But all other factors are not constant . There's no telling what changes based on your decision to use PHP or Java. For example, Java takes longer to develop, which means that Java programmers have their computers on for longer, and their power usage from that may surpass your savings on the server.

If you are Google, Amazon, or some other company who serves literally billions of requests each day from thousands of servers, I would worry about this. Otherwise, your scale isn't large enough to make any positive assertions about energy consumption, so any decision you make is just as likely to be counterproductive as it is to be productive because it's impossible to include all the relevant factors.

A relevant example is a few months back when a rumor went around that you could save energy by setting your desktop background to black. The thinking was that black == no light, so you weren't using as much light. Google (one of the few companies with enough power usage to make this kind of research productive) ran some experiments and did some research, and discovered that LCD screens produce white light no matter what, and filter it by passing a second current through the pixels in a different way. So by setting a pixel to black, you are setting its filtration to the maximum, actually using the most energy possible. The most energy-saving desktop background (at least on an LCD screen) is a white background. However, since white backgrounds hurt people's eyes, few people set their desktop backgrounds to white and the rumor that black backgrounds save energy is still prevalent.

So, to make a long story short, worrying about this is likely to do more harm than good.

This is the same question as what language has the best performance. Now then, for just about every project most programmers ever get in contact with. How you write the system by far outweighs the technologies used (given that the system is speced ok).

The choice of language wrt performance is imho for embedded systems and scientists. You choose language according to what problem to solve, extremely seldom for how cpu efficient it is.

Again, it is how you design and write the system that determines how efficient it will be.

Obviously modern computing techniques use a lot more resources than they used to, and this directly equates to power consumption. Once we'd use a plain old socket to transfer binary data over the network and a 100Mhz processor would handle it, now we have a software stack where the data is converted to XML text, then passed over a web service over http over that same socket, and we wonder why we need a 3Ghz processor with spods of RAM to get decent performance :)

So that's the issue, the only thing you can do about it is to make your code more efficient. Typically this means use a lower level language, and less reliance of general-purpose frameworks or libraries. Definitely don't try to layer software stacks if you can't help it.

Modern programming doesn't like this - there's a push toward programmer productivity, employing cheaper programmers, and re-engineering code all the time (ie rewriting it) so code needs to be easy to create (and sometimes maintain). As a result, to get the best you'll need to trade off these factors. The industry standard way of doing this is simply to mix systems according to their use.

Currently the ultimate performance is C/C++ code, and the ultimate programmer productivity seems to be scripting languages. So, the ideal best of both is to write your main business logic in a script language like Python, and use it to call dedicated 'power helpers' written in C/C++. If you need more performance, you can write more of your code in the underlying C/C++. If you need more RAD fast development, write more in the script language.

My advice for the OP is not to rewrite in Java, it may be better performing overall, but you'll cost so much it might not be worth it. Instead, take the intensive bits of your app and rewrite those as efficiently as you can and call them from your existing PHP. then take a look at your overall architecture and reduce reliance of inefficient protocols and data structures. For example: if you replace XML with JSON, you'll find you have the same functionality but at a fraction of the data size and resources required to parse and reformat the data.

While many of the facets concerning this questions have been already explored in depth here, there is at least one more to look into.

A typical PHP web application duplicates a lot of effort from one request to the next, simply because the PHP execution environment (or context) is not persistent across requests.

Since a typical Java web application does have the ability to persist state directly, without extra steps or cache invalidation, it doesn't need to, for example, perform PHP-style duplicative SQL queries to fetch the same information for each request. Instead, a Java web application often stores the complex information for the current analysis in a native data structure on heap, which is accessed in nanoseconds, rather in milliseconds.

Since the processor has to do significantly less work to perform these basic data access functions, the work requires less power per unit of customer value.

Yes, Java beat PHP (and any other interpreted language) in term of power consumption and speed. But the relation to power consumption and speed is not has trivial has it first seem.

Is a faster language, a greener one?

Traditionally, performance in software languages was in almost all cases synonymous of fast execution time. Recently energy become also a key metric.

The study Energy Efficiency across Programming Languages - How Do Energy, Time, and Memory Relate? highlight that counterintuitively faster is not always greener.

A very common misconception when analyzing energy consumption in software is that it will behave in the same way execution time does. In other words, reducing the execution time of a program would bring about the same amount of energy reduction. In fact, the Energy equation, Energy (J) = Power (W) x Time(s) , indicates that reducing time implies a reduction in the energy consumed. However, the Power variable of the equation, which cannot be assumed as a constant, also has an impact on the energy.

能量、时间和内存的标准化全局结果

Choosing a language if speed and energy are the only factors is easy. But it's not when memory is also an issue.

不同目标组合的帕累托最优集。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM