<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Emre </title>
    <description>The latest articles on DEV Community by Emre  (@zwony).</description>
    <link>https://dev.to/zwony</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3838354%2Fa0265690-cb58-4feb-8c2c-99b28d2acd06.png</url>
      <title>DEV Community: Emre </title>
      <link>https://dev.to/zwony</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zwony"/>
    <language>en</language>
    <item>
      <title>I Built a Python Library to Measure Code's Carbon Footprint — Here's What I Learned</title>
      <dc:creator>Emre </dc:creator>
      <pubDate>Sun, 22 Mar 2026 12:28:49 +0000</pubDate>
      <link>https://dev.to/zwony/i-built-a-python-library-that-measures-your-codes-carbon-footprint-4b91</link>
      <guid>https://dev.to/zwony/i-built-a-python-library-that-measures-your-codes-carbon-footprint-4b91</guid>
      <description>&lt;p&gt;You've probably never asked this question while writing code:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"How much CO₂ did this function just emit?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I hadn't either — until I started building &lt;strong&gt;EcoTrace&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem With Existing Tools
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/mlco2/codecarbon" rel="noopener noreferrer"&gt;CodeCarbon&lt;/a&gt; is great for getting started. &lt;br&gt;
But after using it in production, I kept hitting the same walls:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No async support&lt;/strong&gt; — modern Python is async, tracking shouldn't be sync-only&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;System-wide CPU measurement&lt;/strong&gt; — in a multi-process environment, this is noise&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No AMD/Intel GPU support&lt;/strong&gt; — NVIDIA-only in 2025 is a real limitation&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No side-by-side comparison&lt;/strong&gt; — I wanted to know: is v1 or v2 of my function greener?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So I built EcoTrace.&lt;/p&gt;
&lt;h2&gt;
  
  
  How It Works in 5 Lines
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;ecotrace&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;EcoTrace&lt;/span&gt;

&lt;span class="n"&gt;eco&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;EcoTrace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;region_code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;TR&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nd"&gt;@eco.track&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;train_model&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="c1"&gt;# your training code
&lt;/span&gt;    &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="nf"&gt;train_model&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# [EcoTrace] Duration   : 12.4s
# [EcoTrace] Avg CPU    : 87.3%
# [EcoTrace] CO2 Emitted: 0.00312 gCO2
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  The Feature That Surprised Me Most
&lt;/h2&gt;

&lt;p&gt;Process-scoped CPU measurement. Most tools measure system-wide CPU — &lt;br&gt;
which means if you're running on a shared server, you're measuring &lt;br&gt;
everyone's compute, not yours.&lt;/p&gt;

&lt;p&gt;EcoTrace uses &lt;code&gt;psutil.Process()&lt;/code&gt; to track only your process:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# This measures YOUR code, not the entire machine
&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_current_process&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In a Kubernetes pod running 8 services, this difference is enormous.&lt;/p&gt;

&lt;h2&gt;
  
  
  Async Support — Finally
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@eco.track&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fetch_and_process&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch_from_api&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same decorator. Works on sync and async automatically. &lt;br&gt;
EcoTrace detects coroutines at decoration time and routes accordingly.&lt;/p&gt;
&lt;h2&gt;
  
  
  Comparing Two Implementations
&lt;/h2&gt;

&lt;p&gt;This is my favorite feature:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;bert_inference&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;model_bert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;distilbert_inference&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;model_distilbert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;eco&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bert_inference&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;distilbert_inference&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# BERT:      0.00042 gCO2 | 1.24s
# DistilBERT: 0.00018 gCO2 | 0.51s  ← 57% less carbon
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can make architecture decisions based on emissions, not just speed.&lt;/p&gt;

&lt;h2&gt;
  
  
  40+ Regions, Including Surprising Ones
&lt;/h2&gt;

&lt;p&gt;Sweden (13 gCO₂/kWh) vs Australia (490 gCO₂/kWh) — same code, &lt;br&gt;
&lt;strong&gt;37x different carbon footprint&lt;/strong&gt; depending on where it runs.&lt;/p&gt;

&lt;p&gt;This matters for cloud region selection.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try It
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;ecotrace
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;GitHub: &lt;a href="https://github.com/Zwony/ecotrace" rel="noopener noreferrer"&gt;github.com/Zwony/ecotrace&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'd love feedback — especially from anyone running async workloads &lt;br&gt;
or multi-GPU setups. What carbon metrics matter most to you?__&lt;/p&gt;

</description>
      <category>python</category>
      <category>sustainability</category>
      <category>opensource</category>
      <category>machinelearning</category>
    </item>
  </channel>
</rss>
