mlco2 / codecarbon

Track emissions from Compute and recommend ways to reduce their impact on the environment.
https://mlco2.github.io/codecarbon
MIT License
1.11k stars 173 forks source link

Same energy consumption for different pieces of code #478

Open HinaSharma16 opened 10 months ago

HinaSharma16 commented 10 months ago

Description

I am trying to generate the emergency consumption when we run different types of inefficient code - like unwanted loops, multiple API calls etc. The goal is to find the consumptions and then change the code to make it more sustainable.

Tell us what happened, what went wrong, and what you expected to happen. The problem is whatever code I run, I get the same energy consumption value . i expect different values when different code is run.

What I Did

Pasting my 2 pieces of code that I am trying to run :
Code 1: from codecarbon import EmissionsTracker

Initialize CodeCarbon emissions tracker

tracker = EmissionsTracker()

def inefficient_loop(n): result = 0 for i in range(n): result += i * i return result

Start tracking emissions

tracker.start()

Call the inefficient loop with a large number

result = inefficient_loop(1000000) print(result)

CODE 2 import requests from codecarbon import EmissionsTracker

Initialize CodeCarbon emissions tracker

tracker = EmissionsTracker()

def inefficient_weather_api_calls(api_key, city):

Simulate inefficient API requests by making multiple unnecessary calls

for i in range(5):
    print(i)
    response = requests.get(
        f"http://api.openweathermap.org/data/2.5/weather",
        params={"q": city, "appid": api_key, "units": "metric"},
    )
    # Inefficient: Not using the response data, just making repeated calls
return response

Replace 'YOUR_API_KEY' with your actual OpenWeatherMap API key

api_key = "2222f7ef9f5853ceb124704bcc9f8d44" city = "London"

Start tracking emissions

tracker.start()

Call the inefficient weather API

response = inefficient_weather_api_calls(api_key, city)

Inefficient: Not using the response data, just making repeated calls

for _ in range(3): response = inefficient_weather_api_calls(api_key, city)

Stop tracking emissions and print the estimated carbon footprint

tracker.stop() print(f"Estimated Carbon Footprint: {tracker.final_emissions} gCO2") print(f"Estimated Carbon Footprint: {tracker.final_emissions_data} gCO2")

Stop tracking emissions and print the estimated carbon footprint

tracker.stop() print(f"Estimated Carbon Footprint: {tracker.final_emissions} gCO2")

Paste the command(s) you ran and the output.
If there was a crash, please include the traceback here.

These were the results after running the code :

CODE 1 Results :

333332833333500000 Estimated Carbon Footprint: 1.8371171394348145e-07 gCO2 [codecarbon INFO @ 22:08:56] Energy consumed for RAM : 0.000000 kWh. RAM Power : 12.0 W [codecarbon INFO @ 22:08:56] Energy consumed for all CPUs : 0.000000 kWh. Total CPU Power : 5.0 W [codecarbon INFO @ 22:08:56] 0.000000 kWh of electricity used since the beginning.

CODE 2 : [codecarbon INFO @ 22:09:33] Energy consumed for RAM : 0.000021 kWh. RAM Power : 12.0 W [codecarbon INFO @ 22:09:33] Energy consumed for all CPUs : 0.000009 kWh. Total CPU Power : 5.0 W [codecarbon INFO @ 22:09:33] 0.000030 kWh of electricity used since the beginning. Estimated Carbon Footprint: 1.8994773814803363e-05 gCO2

If you see Carbon emissions are almost same.
Could you please help to make us understand what is wrong here . I need to prepare a report showcasing how inefficient code - loops objects etc can cause higher carbon emissions.

Any changes in system setup is required?

benoit-cty commented 10 months ago

"Carbon emissions are almost same." take care at the scientific notation : You have 1.8e-07 and 1.8e-05 which mean a 100x factor. Do you have the beginning of the trace ? I suspect we use a constant CPU TDP.