Thread: Environmentally responsible coding

  1. #1
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396

    Environmentally responsible coding

    How many of you have considered the environmental impact of the code you write, or code you use on a daily basis?

    Pulling a number from somewhere, a typical PC might draw 200 watts when doing something intensive. At the wall, the inefficiency of the PSU will cause the actual power to be somewhat higher, let's say 300 watts.

    Let's consider some heavily used code. For instance, the JPEG decoder present in all web browsers.

    Suppose there are half a billion active Internet users. Let's also suppose that each user uses the web, and views an average of 5000 JPEGs per day (a typical page might have a hundred or more images on it, so this is probably an underestimate). So, the JPEG decoder executes about 2.5 trillion times per day.

    Suppose it takes on average, 0.02 seconds to decode a JPEG. Remember, some JPEGs are large, some are small.

    Starting from these assumptions, what is the energy impact of a 10% improvement in efficiency of the JPEG decoder?

    2.5e12 * 300 watts * 0.002 seconds = 1500 gigajoules.

    That's the equivalent of burning about 12000 gallons of gasoline every day. In a year, that's 4.4 million gallons.

    Imagine having the ability to spend a few hours optimizing something, and the end result is that 4.4 million fewer gallons of gas are burned each year?

    Something to think about.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Well, I'm pretty sure I'm not always consuming more energy because I'm using a JPEG decoder while my computer is turned on. I don't think that's how the PSU works in our computers, is it? (honest question. I trully don't know). Besides a gain in performance will most of the time mean an increased expense in processor usage... which would also translate to power consumption, no?

    Perhaps efficiency would be a better goal. More time to process if needed be, but at a less overall energy consumption cost.

    But looking at those numbers, and assuming a worldwide concern towards environmental impact of energy consumption, imagine the gain if instead of looking at performance we all turned off our computers when we weren't using them. Or, perhaps even more significant to the point at hand, if we curbed our use of JPEGs/Flash/Client-side scripting/etc... in websites.

    Performance for the benefit of the environment seems like arguing for animal fur coats because synthetic fur coats take longer to biodegrade.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Yes, this is one reason why C is ultimately better than C++ even tho it requires more effort on the part of the programmer.

    I pretty sure it is all trivialized by the amount of "real driving" (read: real driving) that most computer users/owners/programmers do during their normal daily activities.

    I can totally see some absurd future greenwash marketing whereby browser X is sold as more environmentally friendly.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Mario F. View Post
    Well, I'm pretty sure I'm not always consuming more energy because I'm using a JPEG decoder while my computer is turned on. I don't think that's how the PSU works in our computers, is it? (honest question. I trully don't know). Besides a gain in performance will most of the time mean an increased expense in processor usage... which would also translate to power consumption, no?
    I've actually measured it with a wattmeter at the wall. There's a significant difference between an idle CPU and a maxed-out CPU.

    As far as performance gain actually taking MORE power, it depends how it's implemented in the first place. It's easy to max out a CPU by doing unnecessary work. Simply removing the unnecessary work doesn't reduce the power, but it does reduce the amount of time that power is required for.

    Perhaps efficiency would be a better goal. More time to process if needed be, but at a less overall energy consumption cost.
    That's strictly more accurate, but it requires knowledge of how much power the various parts of a CPU consume. For instance, if you know that the SSE3 unit takes five times the power as standard floating point, you might choose floating point even if it makes the code run three times slower.

    But looking at those numbers, and assuming a worldwide concern towards environmental impact of energy consumption, imagine the gain if instead of looking at performance we all turned off our computers when we weren't using them. Or, perhaps even more significant to the point at hand, if we curbed our use of JPEGs/Flash/Client-side scripting/etc... in websites.
    You're right of course, but the reality is you simply can't MAKE people do it. But you can tweak a piece of code that's used worldwide, and eventually everybody will be running it, through no deliberate action of their own. You made the difference all by yourself instead of needing to convince a billion people to turn off their computers.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Mario F. View Post
    Client-side scripting
    I am guessing that moving the load server side will reduce the "carbon footprint" in your house, but result in an overall increase because of the necessary server upgrades. Maybe you can then sell your carbon credits to the sites you visit, so everybody can think* something has been accomplished.

    If you think that people will go for a "less is more web" then maybe you also believe they will go for a "less is more" transit style, or "less is more" packaging at the supermarket.

    The last two have been beyond absolute total and complete failures for decades, as would this. People do more gas gussling and consume more pure garbage, per capita, now, than ever before. So again, the only real "accomplishment" to attain would be an advertising gimmick.

    * to use the word loosely
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I don't believe in anything. I was purposely exposing the irony of the situation. I agree entirely with you two that is just not how the world eventually works. But I felt the need to remember one less JPEG in a 10 JPEGs webpage, will have a greater impact than a 10% increase in the decoder efficiency. The irony resides in the fact that's just not a good solution anymore.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    I think Javascript and Flash advertisements are the predominant causes of high CPU usage.

    I always browse with JS turned off, which makes websites load faster(and reduces the risk of malicious JS embedded in the webpage). I only turn JS on if particular sites require it, like for banking.
    OS: Linux Mint 13(Maya) LTS 64 bit.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I remember reading in the Linux kernel changelogs at some point that they changed the architecture a little bit so that a wakeup timer no longer fired every second. After this change you could actually let your computer sit there while it was running and get many fewer CPU wakeups per second (theoretically zero, but most other devices wake it up every so often as well). I thought that was a great idea at the time, because if you use powertop for a while you start to realize how much power you save by keeping the CPU in standby mode as long as you possibly can . . . .

    I imagine that sort of change had a measurable impact on power consumption (at least for Linux users), because it's in the kernel and it applies all of the time. Anyway, it was just what came to mind when I read this thread.

    [I should mention that I probably got half the terms there mixed up, I really don't know very much about the subject . . . .]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Never thought about it. Probably won't a few seconds after reading this either.

    Most oil drums: 55 US gallons
    Last estimate of leak in the Gulf: ~5000 barrels a minute or 275,000 gallons per minute, 16,500,000 per hour, 396,000,000 per day.

    Total required for 1 hour of use for half a billion users with usage of 12,000 gallons per day: 500 gallons per hour
    Percent of power required out of total in one hour given a flow rate of 275,000 gallons per minute (16,500,000 per hour) and usage of 500 per hour for half a billion users: 500 / 16,500,000 = 3.030~030 * e^-5%.

    So that one leak in the Gulf produces enough oil in one hour to power half a billion users with a usage of 12,000 gallons per day for 33,000 (396,000,000 / 12,000) days or 90.14 years.

    And besides electrical power isn't normally produced from oil, it is produced from coal and/or nuclear and/or water.
    Last edited by VirtualAce; 05-06-2010 at 07:51 PM.

  10. #10
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Microsoft should really do something for the environment.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Brafil View Post
    Microsoft should really do something for the environment.
    So should the makers of Hostess potato chips, etc, but I think the most you will learn about "social responsibility" in business school is how to hire a PR firm to clean your exposure/profile while minimizing any other possible expenditure.

    Capitalist enterprise, by it's very nature, is simply not capable of "doing something for the environment". If this were not the case, it would have happened by now, but it has not and it won't be in the future, either.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Ok a little update on the numbers. Recent reports say the leak is around 4 million gallons. That does not equate to the aforementioned 5000 barrels a minute. So once again the first reports coming out were completely exaggerated and the more recent reports are still exaggerated but getting closer to the actual number...if that is even possible given the conditions.

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MK27 View Post
    So should the makers of Hostess potato chips, etc, but I think the most you will learn about "social responsibility" in business school is how to hire a PR firm to clean your exposure/profile while minimizing any other possible expenditure.
    Part of the point I'm making is because of the nature of software -- created once and then deployed without bound -- a single developer can make a large difference. Even if that difference is insignificant on a global energy balance, the rate of return per person involved is huge.

    How much impact can you make by selling your car and riding a bike? How much by installing energy efficient appliances? By turning things off and unplugging them when not in use? Can you make 4.4 million gallons difference?

    I'm not saying that "green" programming will save the world.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by brewbuck View Post
    Part of the point I'm making is because of the nature of software -- created once and then deployed without bound -- a single developer can make a large difference.
    Sure, and you could write CLI interfaces more too, that would make a way more huge difference in terms of resource usage. But let's be realistic -- the environment is not worth people having to use the command line!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Course not. That would require many hours of additional computer courses for most of the users, which means more fuel is wasted for transport.

    But maybe... Microsoft could make a light windows system with nice graphics (XP-Style) and low system requirements...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP - Stuck on some coding
    By trueman1991 in forum C Programming
    Replies: 6
    Last Post: 10-29-2009, 12:54 PM
  2. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  3. Coding Guideline ....!!
    By imfeelingfortun in forum Tech Board
    Replies: 8
    Last Post: 10-08-2006, 07:09 AM
  4. Before Coding
    By cyberCLoWn in forum C++ Programming
    Replies: 16
    Last Post: 12-15-2003, 02:26 AM
  5. Coding Contest....
    By Koshare in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 10-14-2001, 04:32 PM