<?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: Martin Pi</title>
    <description>The latest articles on DEV Community by Martin Pi (@martin_pi).</description>
    <link>https://dev.to/martin_pi</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%2F2315172%2Ff67f844e-533f-4121-91f5-0d588179965f.jpg</url>
      <title>DEV Community: Martin Pi</title>
      <link>https://dev.to/martin_pi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/martin_pi"/>
    <language>en</language>
    <item>
      <title>Comprendiendo la Complejidad Ciclomática en Desarrollo de Software</title>
      <dc:creator>Martin Pi</dc:creator>
      <pubDate>Tue, 18 Mar 2025 18:15:26 +0000</pubDate>
      <link>https://dev.to/martin_pi/comprendiendo-la-complejidad-ciclomatica-en-desarrollo-de-software-2fc1</link>
      <guid>https://dev.to/martin_pi/comprendiendo-la-complejidad-ciclomatica-en-desarrollo-de-software-2fc1</guid>
      <description>&lt;p&gt;La &lt;strong&gt;complejidad ciclomática&lt;/strong&gt; es una métrica clave utilizada para medir la complejidad estructural del código. Fue introducida por Thomas J. McCabe en 1976 y se utiliza para evaluar la cantidad de caminos lógicos posibles en un programa. Esta métrica no solo ayuda a identificar el nivel de dificultad para probar y mantener el código, sino que también indica la probabilidad de errores.&lt;/p&gt;

&lt;p&gt;En este artículo, exploraremos qué es la complejidad ciclomática, su cálculo, los "números mágicos" asociados, y cómo usar esta métrica para mejorar la calidad del código. También incluiremos ejemplos en Java para ilustrar cómo medir y analizar la complejidad ciclomática.&lt;/p&gt;




&lt;h2&gt;
  
  
  ¿Qué es la Complejidad Ciclomática?
&lt;/h2&gt;

&lt;p&gt;La complejidad ciclomática mide el número de caminos linealmente independientes a través del código fuente de un programa. Se calcula a partir del flujo de control de un programa, basándose en el número de nodos, aristas y componentes conectados en el gráfico.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fórmula
&lt;/h3&gt;

&lt;p&gt;La fórmula básica para calcular la complejidad ciclomática es:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;M = E - N + 2P
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Donde:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;M&lt;/strong&gt;: Complejidad ciclomática.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;E&lt;/strong&gt;: Número de aristas en el gráfico.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;N&lt;/strong&gt;: Número de nodos en el gráfico.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;P&lt;/strong&gt;: Número de componentes conectados (en la mayoría de los casos es 1).&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ¿Por qué es Importante?
&lt;/h2&gt;

&lt;p&gt;La complejidad ciclomática ayuda a:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Identificar código difícil de mantener:&lt;/strong&gt; Los valores altos indican un mayor riesgo de errores.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Determinar la cantidad mínima de casos de prueba:&lt;/strong&gt; Proporciona una guía para garantizar una cobertura adecuada de pruebas.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Promover la simplicidad:&lt;/strong&gt; Incentiva la escritura de código más claro y modular.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Los "Números Mágicos"
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Límites Recomendados
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;1 a 10:&lt;/strong&gt; Código simple y manejable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;11 a 20:&lt;/strong&gt; Código moderadamente complejo; se recomienda refactorización si es posible.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;21 a 50:&lt;/strong&gt; Código complejo; difícil de probar y mantener.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Más de 50:&lt;/strong&gt; Código extremadamente complejo; requiere una reescritura completa.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Ejemplo en Java
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Ejemplo&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;calcularMaximo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// Camino 1&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// Camino 2&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// Camino 3&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Cálculo de la Complejidad Ciclomática
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Nodos:&lt;/strong&gt; 4 (inicio, tres ramas de control).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Aristas:&lt;/strong&gt; 5.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Componentes conectados:&lt;/strong&gt; 1.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;M = E - N + 2P = 5 - 4 + 2(1) = 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Este método tiene una complejidad ciclomática de &lt;strong&gt;3&lt;/strong&gt;, lo que indica tres caminos lógicos independientes.&lt;/p&gt;




&lt;h2&gt;
  
  
  Métricas de Código
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Otros Indicadores Importantes
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Número de líneas de código (LOC):&lt;/strong&gt; Ayuda a evaluar el tamaño general del código.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Número de métodos:&lt;/strong&gt; Proporciona una idea del nivel de modularidad.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Profundidad de anidación:&lt;/strong&gt; Una anidación excesiva aumenta la complejidad y dificulta la legibilidad.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Análisis de Código
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Herramientas para Medir la Complejidad Ciclomática
&lt;/h3&gt;

&lt;p&gt;Existen herramientas que pueden calcular automáticamente la complejidad ciclomática:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SonarQube:&lt;/strong&gt; Proporciona informes detallados sobre la calidad del código.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Eclipse Metrics Plugin:&lt;/strong&gt; Un complemento para calcular métricas directamente en el IDE.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CodeClimate:&lt;/strong&gt; Ofrece un análisis profundo de la calidad del código.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Ejemplo Avanzado en Java
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ValidacionUsuario&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;validarUsuario&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;nombre&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;edad&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nombre&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;nombre&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// Camino 1&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;edad&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;edad&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// Camino 2&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;contains&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"@"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// Camino 3&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Camino 4&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Nodos:&lt;/strong&gt; 5.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Aristas:&lt;/strong&gt; 6.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Componentes conectados:&lt;/strong&gt; 1.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;M = E - N + 2P = 6 - 5 + 2(1) = 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Este método tiene una complejidad ciclomática de &lt;strong&gt;3&lt;/strong&gt;, lo que lo hace manejable.&lt;/p&gt;




&lt;h2&gt;
  
  
  Cómo Reducir la Complejidad Ciclomática
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Dividir funciones grandes:&lt;/strong&gt; Extraer métodos pequeños para reducir la complejidad.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Evitar estructuras de control anidadas:&lt;/strong&gt; Utilizar técnicas como guard clauses.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Adoptar un enfoque modular:&lt;/strong&gt; Promueve la reutilización y mejora la legibilidad.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Ejemplo: Refactorización
&lt;/h3&gt;

&lt;p&gt;Código Original:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;validar&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Código Refactorizado:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;validar&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reducción de la complejidad sin afectar la lógica.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusión
&lt;/h2&gt;

&lt;p&gt;La complejidad ciclomática es una métrica esencial para evaluar la calidad del código. Entender y medir esta métrica permite identificar áreas problemáticas, mejorar la mantenibilidad y garantizar una cobertura de pruebas adecuada. Adoptar herramientas y prácticas recomendadas ayuda a mantener un código más limpio, simple y eficiente.&lt;/p&gt;




&lt;h2&gt;
  
  
  Referencias
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Cyclomatic_complexity" rel="noopener noreferrer"&gt;Cyclomatic Complexity: A Practical Guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.sonarqube.org/" rel="noopener noreferrer"&gt;SonarQube Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.jetbrains.com/help/idea/code-metrics.html" rel="noopener noreferrer"&gt;Code Metrics for Java Developers&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Big-O en Español Parte 5</title>
      <dc:creator>Martin Pi</dc:creator>
      <pubDate>Tue, 18 Mar 2025 18:07:12 +0000</pubDate>
      <link>https://dev.to/martin_pi/big-o-en-espanol-parte-5-455e</link>
      <guid>https://dev.to/martin_pi/big-o-en-espanol-parte-5-455e</guid>
      <description>&lt;p&gt;En los artículos anteriores sobre la notación Big-O, exploramos diversas complejidades, desde constantes y lineales hasta factoriales y exponenciales. Sin embargo, hay muchas otras consideraciones importantes cuando se trata de analizar y optimizar algoritmos.&lt;/p&gt;

&lt;p&gt;En este artículo, abordaremos temas avanzados que complementan los conceptos básicos de Big-O.&lt;/p&gt;




&lt;h2&gt;
  
  
  ¿Qué encontrarás en esta serie?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/martin_pi/big-o-en-espanol-parte-3-4j17#"&gt;Parte 1: Expresiones Lineales y Constantes&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Introducción a las notaciones más simples y cómo afectan el rendimiento de nuestros algoritmos.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/martin_pi/big-o-en-espanol-parte-2-4kpk"&gt;Parte 2: Expresiones Cuadráticas, Cúbicas y de Otras Potencias&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Un análisis más profundo sobre cómo crecen los tiempos de ejecución en algoritmos más complejos.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/martin_pi/big-o-en-espanol-parte-3-4j17"&gt;Parte 3: Expresiones Logarítmicas, Exponenciales y Factoriales&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Exploraremos casos donde el tiempo de ejecución crece de forma mucho más drástica.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/martin_pi/big-o-en-espanol-parte-4-58ff"&gt;Parte 4: Combinando Complejidades y Comparando Estructuras de Datos&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Analizaremos cómo diferentes estructuras de datos afectan la complejidad y cómo combinar varios algoritmos.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/martin_pi/big-o-en-espanol-parte-5-455e"&gt;Parte 5: Consideraciones Avanzadas y Casos Reales&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Reflexiones finales sobre trade-offs entre tiempo y espacio, complejidades amortizadas, y cómo aplicar Big-O en escenarios reales.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Consideraciones Adicionales en Big-O
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Complejidad Espacial (Space Complexity)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Además de medir el tiempo de ejecución de un algoritmo, también es fundamental considerar la &lt;strong&gt;complejidad espacial&lt;/strong&gt;, que mide cuánta memoria adicional requiere un algoritmo mientras se ejecuta.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ejemplo:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Espacial constante (O(1)):&lt;/strong&gt; No se necesita memoria adicional que dependa del tamaño de los datos de entrada.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Espacial lineal (O(n)):&lt;/strong&gt; Requiere memoria proporcional al tamaño de los datos de entrada.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;calcularSuma&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arreglo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;suma&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Espacial constante&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arreglo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;suma&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;arreglo&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// Solo usamos una variable adicional para almacenar la suma&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;suma&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;En este caso, la complejidad espacial es &lt;strong&gt;O(1)&lt;/strong&gt;, ya que no se crean estructuras adicionales proporcionales al tamaño de los datos.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. &lt;strong&gt;Trade-off entre Tiempo y Espacio&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;En muchos casos, optimizar el tiempo de ejecución de un algoritmo puede incrementar su complejidad espacial, y viceversa. Este equilibrio debe analizarse en función de los recursos disponibles.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ejemplo: Almacenamiento en caché (memoization)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;memo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// Uso de memoria adicional&lt;/span&gt;
  &lt;span class="nx"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Este enfoque mejora el tiempo de ejecución al reducir llamadas redundantes, pero utiliza memoria adicional para almacenar los resultados.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. &lt;strong&gt;Complejidad Amortizada&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;La complejidad amortizada evalúa el costo promedio de una operación en un conjunto de datos después de realizar varias operaciones.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ejemplo: Arrays Dinámicos&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Los arreglos dinámicos, como los de JavaScript, duplican su tamaño cuando se excede su capacidad. Aunque la inserción parece &lt;strong&gt;O(n)&lt;/strong&gt; al duplicarse, la complejidad promedio es &lt;strong&gt;O(1)&lt;/strong&gt; debido a la rareza de esta operación de redimensionamiento.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arreglo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="nx"&gt;arreglo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// O(1)&lt;/span&gt;
&lt;span class="nx"&gt;arreglo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// O(1)&lt;/span&gt;
&lt;span class="nx"&gt;arreglo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Si se excede la capacidad, se copia en un arreglo más grande (O(n))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  4. &lt;strong&gt;Casos Especiales: Mejor, Peor y Promedio Caso&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;El análisis de Big-O puede variar según los datos de entrada. Es importante entender los tres casos:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Mejor caso:&lt;/strong&gt; El tiempo mínimo que toma un algoritmo. Ejemplo: Buscar un elemento que está al inicio de un arreglo.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Peor caso:&lt;/strong&gt; El tiempo máximo que toma un algoritmo. Ejemplo: Buscar un elemento que no está en un arreglo.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caso promedio:&lt;/strong&gt; Tiempo promedio considerando datos de entrada aleatorios.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Ejemplo: Búsqueda lineal&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;buscarElemento&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arreglo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;elemento&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arreglo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arreglo&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;elemento&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Mejor caso: O(1)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Peor caso: O(n)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  5. &lt;strong&gt;Impacto de la Constante Oculta&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;La notación Big-O ignora las constantes y los términos de menor grado, pero en la práctica, estas constantes pueden tener un impacto significativo.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ejemplo: Algoritmo Bubble Sort vs Quick Sort&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Aunque ambos algoritmos tienen la misma complejidad en el peor caso (&lt;strong&gt;O(n²)&lt;/strong&gt;), Quick Sort es mucho más eficiente en promedio debido a su menor constante oculta.&lt;/p&gt;




&lt;h3&gt;
  
  
  6. &lt;strong&gt;Paralelismo y Concurrencia&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;En sistemas modernos, el paralelismo y la concurrencia pueden reducir significativamente el tiempo de ejecución, aunque no cambian la notación Big-O.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ejemplo: Procesamiento en paralelo&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Si un algoritmo procesa datos en paralelo, su tiempo de ejecución puede dividirse entre el número de hilos o procesadores.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;procesarDatosEnParalelo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;datos&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;resultados&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;datos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dato&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;procesar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dato&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Operación paralela en sistemas distribuidos&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;resultados&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Conclusión
&lt;/h2&gt;

&lt;p&gt;Big-O es una herramienta esencial para entender la eficiencia de los algoritmos, pero no es el único factor a considerar. Complejidades espaciales, análisis de casos especiales, constantes ocultas y el impacto del paralelismo son aspectos importantes que complementan el análisis de eficiencia.&lt;/p&gt;

&lt;p&gt;Con estos conceptos avanzados, puedes analizar algoritmos de manera más profunda y tomar decisiones informadas al diseñar soluciones escalables.&lt;/p&gt;




&lt;h2&gt;
  
  
  Referencias
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.bigocheatsheet.com/" rel="noopener noreferrer"&gt;Notación Big-O - Guía Completa&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/architecture/patterns/space-complexity" rel="noopener noreferrer"&gt;Introducción a la Complejidad Espacial&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://es.wikipedia.org/wiki/An%C3%A1lisis_de_algoritmos" rel="noopener noreferrer"&gt;Algoritmos y Complejidad Computacional&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Big-O en Español - Parte 4</title>
      <dc:creator>Martin Pi</dc:creator>
      <pubDate>Tue, 18 Mar 2025 18:05:07 +0000</pubDate>
      <link>https://dev.to/martin_pi/big-o-en-espanol-parte-4-58ff</link>
      <guid>https://dev.to/martin_pi/big-o-en-espanol-parte-4-58ff</guid>
      <description>&lt;p&gt;Este es el último post de la serie sobre la notación Big-O. En este artículo, analizaremos el rendimiento de cada tipo de expresión y aprenderemos cómo combinar diferentes complejidades en un algoritmo.&lt;/p&gt;




&lt;h2&gt;
  
  
  ¿Qué encontrarás en esta serie?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/martin_pi/big-o-en-espanol-parte-3-4j17#"&gt;Parte 1: Expresiones Lineales y Constantes&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Introducción a las notaciones más simples y cómo afectan el rendimiento de nuestros algoritmos.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/martin_pi/big-o-en-espanol-parte-2-4kpk"&gt;Parte 2: Expresiones Cuadráticas, Cúbicas y de Otras Potencias&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Un análisis más profundo sobre cómo crecen los tiempos de ejecución en algoritmos más complejos.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/martin_pi/big-o-en-espanol-parte-3-4j17"&gt;Parte 3: Expresiones Logarítmicas, Exponenciales y Factoriales&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Exploraremos casos donde el tiempo de ejecución crece de forma mucho más drástica.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/martin_pi/big-o-en-espanol-parte-4-58ff"&gt;Parte 4: Combinando Complejidades y Comparando Estructuras de Datos&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Analizaremos cómo diferentes estructuras de datos afectan la complejidad y cómo combinar varios algoritmos.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/martin_pi/big-o-en-espanol-parte-5-455e"&gt;Parte 5: Consideraciones Avanzadas y Casos Reales&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Reflexiones finales sobre trade-offs entre tiempo y espacio, complejidades amortizadas, y cómo aplicar Big-O en escenarios reales.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Gráfico de Complejidad de Big-O
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpdoclmkbod2mj0qpxyst.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpdoclmkbod2mj0qpxyst.png" alt="Image description" width="800" height="512"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Referencia:&lt;/strong&gt; &lt;a href="https://www.bigocheatsheet.com/" rel="noopener noreferrer"&gt;Big-O Cheat Sheet&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Como vimos en los posts anteriores, las expresiones constantes (&lt;strong&gt;O(1)&lt;/strong&gt;) y logarítmicas (&lt;strong&gt;O(Log n)&lt;/strong&gt;) son las de mejor rendimiento, mientras que las exponenciales (&lt;strong&gt;O(2^n)&lt;/strong&gt;) y factoriales (&lt;strong&gt;O(n!)&lt;/strong&gt;) son las menos eficientes.&lt;/p&gt;




&lt;h2&gt;
  
  
  Complejidad de Algoritmos con Números
&lt;/h2&gt;

&lt;p&gt;La siguiente tabla ilustra cómo aumenta el tiempo de procesamiento a medida que crece el número de elementos:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Notación Big-O&lt;/th&gt;
&lt;th&gt;10 elementos&lt;/th&gt;
&lt;th&gt;100 elementos&lt;/th&gt;
&lt;th&gt;1000 elementos&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;1000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;O(Log n)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;O(n²)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;10,000&lt;/td&gt;
&lt;td&gt;1,000,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;O(2^n)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;1024&lt;/td&gt;
&lt;td&gt;1.27e+30&lt;/td&gt;
&lt;td&gt;1.07e+301&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;O(n!)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;3,628,800&lt;/td&gt;
&lt;td&gt;9.33e+157&lt;/td&gt;
&lt;td&gt;Infinito&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Nota:&lt;/strong&gt; Estos valores no representan tiempos en segundos. Gracias a los procesadores modernos, incluso los algoritmos mal diseñados pueden ejecutarse en milisegundos para volúmenes de datos pequeños. Sin embargo, es crucial entender cómo el crecimiento exponencial o factorial puede impactar significativamente nuestros sistemas cuando los datos aumentan.&lt;/p&gt;




&lt;h2&gt;
  
  
  Operaciones Según el Tipo de Estructura de Datos
&lt;/h2&gt;

&lt;p&gt;La siguiente tabla muestra la notación Big-O asociada a diferentes estructuras de datos:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F48nfjtv7pgpdwzl1bq9i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F48nfjtv7pgpdwzl1bq9i.png" alt="Image description" width="800" height="430"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Referencia:&lt;/strong&gt; &lt;a href="https://www.bigocheatsheet.com/" rel="noopener noreferrer"&gt;Big-O Cheat Sheet&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Comparación de Arreglo, Pila (Stack) y Cola (Queue)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Acceso:&lt;/strong&gt; Los arreglos tienen mejor rendimiento en acceso aleatorio (&lt;strong&gt;O(1)&lt;/strong&gt;) en comparación con pilas y colas.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Inserción y Eliminación:&lt;/strong&gt; Las pilas y colas son más eficientes (&lt;strong&gt;O(1)&lt;/strong&gt;) para estas operaciones.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Búsqueda:&lt;/strong&gt; Para los tres casos, la búsqueda tiene una complejidad lineal (&lt;strong&gt;O(n)&lt;/strong&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Es esencial comprender estas diferencias para tomar decisiones informadas al seleccionar estructuras de datos.&lt;/p&gt;




&lt;h2&gt;
  
  
  ¿Qué Pasa Cuando un Código Tiene Múltiples Complejidades?
&lt;/h2&gt;

&lt;p&gt;Si un algoritmo combina diferentes complejidades, siempre se considera la &lt;strong&gt;mayor&lt;/strong&gt; al evaluar el rendimiento general.&lt;/p&gt;

&lt;p&gt;Por ejemplo:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;O(1) 
O(n) 
O(n²)

O = O(1) + O(n) + O(n²) = O(n²)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;En términos matemáticos:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f(x) = 1 + x + x²
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Gráfica
&lt;/h3&gt;

&lt;p&gt;La gráfica de esta función muestra cómo domina la complejidad cuadrática en comparación con las otras:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk0u146d4i971g8q96s4a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk0u146d4i971g8q96s4a.png" alt="Image description" width="463" height="393"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusión
&lt;/h2&gt;

&lt;p&gt;Con este artículo, cerramos la serie sobre Big-O. Ahora tienes una visión integral de cómo analizar y optimizar algoritmos basándote en su complejidad computacional. &lt;/p&gt;

&lt;p&gt;¡Gracias por leer! Si te gustó esta serie, no dudes en compartirla con otros desarrolladores interesados en mejorar su comprensión de algoritmos.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Big-O en Español - Parte 3</title>
      <dc:creator>Martin Pi</dc:creator>
      <pubDate>Tue, 18 Mar 2025 17:59:19 +0000</pubDate>
      <link>https://dev.to/martin_pi/big-o-en-espanol-parte-3-4j17</link>
      <guid>https://dev.to/martin_pi/big-o-en-espanol-parte-3-4j17</guid>
      <description>&lt;p&gt;Este es el tercer post de una serie de 5 sobre la notación Big-O. En este artículo, exploraremos las &lt;strong&gt;expresiones logarítmicas&lt;/strong&gt;, &lt;strong&gt;exponenciales&lt;/strong&gt; y &lt;strong&gt;factoriales&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  ¿Qué encontrarás en esta serie?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/martin_pi/big-o-en-espanol-parte-3-4j17#"&gt;Parte 1: Expresiones Lineales y Constantes&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Introducción a las notaciones más simples y cómo afectan el rendimiento de nuestros algoritmos.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/martin_pi/big-o-en-espanol-parte-2-4kpk"&gt;Parte 2: Expresiones Cuadráticas, Cúbicas y de Otras Potencias&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Un análisis más profundo sobre cómo crecen los tiempos de ejecución en algoritmos más complejos.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/martin_pi/big-o-en-espanol-parte-3-4j17"&gt;Parte 3: Expresiones Logarítmicas, Exponenciales y Factoriales&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Exploraremos casos donde el tiempo de ejecución crece de forma mucho más drástica.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/martin_pi/big-o-en-espanol-parte-4-58ff"&gt;Parte 4: Combinando Complejidades y Comparando Estructuras de Datos&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Analizaremos cómo diferentes estructuras de datos afectan la complejidad y cómo combinar varios algoritmos.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/martin_pi/big-o-en-espanol-parte-5-455e"&gt;Parte 5: Consideraciones Avanzadas y Casos Reales&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Reflexiones finales sobre trade-offs entre tiempo y espacio, complejidades amortizadas, y cómo aplicar Big-O en escenarios reales.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Expresiones Logarítmicas
&lt;/h2&gt;

&lt;p&gt;Un ejemplo clásico de una expresión logarítmica es la &lt;strong&gt;búsqueda binaria&lt;/strong&gt;, uno de los primeros algoritmos que aprendemos en estructuras de datos. Este método requiere un arreglo &lt;strong&gt;ordenado&lt;/strong&gt; para funcionar.&lt;/p&gt;

&lt;p&gt;Ejemplo de arreglo:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;vector&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;39&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explicación
&lt;/h3&gt;

&lt;p&gt;Supongamos que queremos buscar el número &lt;strong&gt;7&lt;/strong&gt;. En la búsqueda binaria:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Dividimos el arreglo por la mitad.&lt;/li&gt;
&lt;li&gt;Comparamos el número buscado con el valor en la posición central.&lt;/li&gt;
&lt;li&gt;Si el número buscado es menor, seguimos con la mitad izquierda; si es mayor, usamos la mitad derecha.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Resultado:&lt;/strong&gt; El número &lt;strong&gt;7&lt;/strong&gt; está en el arreglo.&lt;/p&gt;

&lt;p&gt;Si buscamos el número &lt;strong&gt;34&lt;/strong&gt;, aplicamos la misma técnica, pero el número no se encontrará después de iterar.&lt;/p&gt;

&lt;h3&gt;
  
  
  Código de Búsqueda Binaria
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;busquedaBinaria&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arregloNumeros&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;numEncontrar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;izquierda&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;derecha&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arregloNumeros&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;izquierda&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;derecha&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;medio&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;izquierda&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;derecha&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arregloNumeros&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;medio&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;numEncontrar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;medio&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numEncontrar&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arregloNumeros&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;medio&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="nx"&gt;derecha&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;medio&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nx"&gt;izquierda&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;medio&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Número no encontrado&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Complejidad Matemática
&lt;/h3&gt;

&lt;p&gt;La búsqueda binaria es eficiente porque no revisa todos los elementos, sino que divide el arreglo iterativamente. Matemáticamente, se representa como:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f(x) = Log x
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;En notación Big-O:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;O(Log n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Gráfica
&lt;/h3&gt;

&lt;p&gt;La gráfica de estas expresiones es:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9v2xs71chcaqr1xb0olb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9v2xs71chcaqr1xb0olb.png" alt="Image description" width="490" height="395"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Expresiones Exponenciales
&lt;/h2&gt;

&lt;p&gt;Un ejemplo típico de una expresión exponencial es el cálculo recursivo de la &lt;strong&gt;serie de Fibonacci&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fórmula de Fibonacci
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;F(n) = F(n-1) + F(n-2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Código
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;serieFibonacci&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numero&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numero&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;numero&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;serieFibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numero&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;serieFibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numero&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Este algoritmo calcula la serie recursivamente y, debido a que realiza muchas llamadas repetidas, es muy ineficiente para valores altos de &lt;strong&gt;n&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Complejidad Matemática
&lt;/h3&gt;

&lt;p&gt;Matemáticamente, se representa como:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f(x) = 2^x
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;En notación Big-O:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;O(2^n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Gráfica
&lt;/h3&gt;

&lt;p&gt;La gráfica para este tipo de expresiones es:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk352d7gk9jsw5xlvpg8a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk352d7gk9jsw5xlvpg8a.png" alt="Image description" width="466" height="461"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Expresiones Factoriales
&lt;/h2&gt;

&lt;p&gt;Las &lt;strong&gt;expresiones factoriales&lt;/strong&gt; son las de mayor complejidad y se observan en algoritmos que prueban todas las combinaciones posibles, como los &lt;strong&gt;ataques de fuerza bruta&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Ejemplo: Ataques de Fuerza Bruta
&lt;/h3&gt;

&lt;p&gt;Imagina que queremos adivinar una contraseña probando todas las combinaciones de letras y números. Este enfoque es ineficiente y puede tardar días, semanas o meses dependiendo de la longitud de la contraseña.&lt;/p&gt;

&lt;p&gt;Aunque los ataques de fuerza bruta son ineficientes, en ocasiones se diseñan algoritmos similares para explorar combinaciones donde el tiempo no es una restricción crítica.&lt;/p&gt;

&lt;h3&gt;
  
  
  Complejidad Matemática
&lt;/h3&gt;

&lt;p&gt;Matemáticamente, se representa como:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f(x) = x!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;En notación Big-O:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;O(n!)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Gráfica
&lt;/h3&gt;

&lt;p&gt;La gráfica de este tipo de algoritmos muestra un crecimiento extremadamente rápido:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F65mjh19ylezsofs9po8c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F65mjh19ylezsofs9po8c.png" alt="Image description" width="454" height="408"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusión
&lt;/h2&gt;

&lt;p&gt;En este post, hemos explorado:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Expresiones logarítmicas (búsqueda binaria): &lt;strong&gt;O(Log n)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Expresiones exponenciales (Fibonacci recursivo): &lt;strong&gt;O(2^n)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Expresiones factoriales (ataques de fuerza bruta): &lt;strong&gt;O(n!)&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;En la próxima y última parte, exploraremos técnicas para &lt;strong&gt;optimizar algoritmos&lt;/strong&gt; y evitar complejidades altas. ¡No te lo pierdas!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Big-O en Español - Parte 2</title>
      <dc:creator>Martin Pi</dc:creator>
      <pubDate>Mon, 13 Jan 2025 12:42:18 +0000</pubDate>
      <link>https://dev.to/martin_pi/big-o-en-espanol-parte-2-4kpk</link>
      <guid>https://dev.to/martin_pi/big-o-en-espanol-parte-2-4kpk</guid>
      <description>&lt;p&gt;En el post previo, revisamos la notación de Big-O para expresiones lineales y constantes. En este post vamos a explorar las expresiones &lt;strong&gt;cuadráticas&lt;/strong&gt; y cómo se comportan las funciones elevadas a cualquier potencia.&lt;/p&gt;




&lt;h2&gt;
  
  
  ¿Qué encontrarás en esta serie?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/martin_pi/big-o-en-espanol-parte-3-4j17#"&gt;Parte 1: Expresiones Lineales y Constantes&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Introducción a las notaciones más simples y cómo afectan el rendimiento de nuestros algoritmos.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/martin_pi/big-o-en-espanol-parte-2-4kpk"&gt;Parte 2: Expresiones Cuadráticas, Cúbicas y de Otras Potencias&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Un análisis más profundo sobre cómo crecen los tiempos de ejecución en algoritmos más complejos.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/martin_pi/big-o-en-espanol-parte-3-4j17"&gt;Parte 3: Expresiones Logarítmicas, Exponenciales y Factoriales&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Exploraremos casos donde el tiempo de ejecución crece de forma mucho más drástica.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/martin_pi/big-o-en-espanol-parte-4-58ff"&gt;Parte 4: Combinando Complejidades y Comparando Estructuras de Datos&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Analizaremos cómo diferentes estructuras de datos afectan la complejidad y cómo combinar varios algoritmos.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/martin_pi/big-o-en-espanol-parte-5-455e"&gt;Parte 5: Consideraciones Avanzadas y Casos Reales&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Reflexiones finales sobre trade-offs entre tiempo y espacio, complejidades amortizadas, y cómo aplicar Big-O en escenarios reales.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Expresiones Cuadráticas
&lt;/h2&gt;

&lt;p&gt;Vamos a ilustrar esta notación con un ejemplo práctico.&lt;/p&gt;

&lt;p&gt;Supongamos que tenemos un arreglo con todas las selecciones de fútbol masculino que han salido campeonas del mundo desde 1930. Este arreglo incluye el nombre del país tantas veces como mundiales ganó:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;campeonesMundiales&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Uruguay&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Italia&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Italia&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Uruguay&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alemania&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Brasil&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Brasil&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Inglaterra&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Brasil&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alemania&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Argentina&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Italia&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Argentina&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alemania&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Brasil&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Francia&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Italia&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;España&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alemania&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Francia&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ahora supongamos que tenemos otro arreglo con todas las selecciones que participaron en el Mundial de Qatar 2022:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;seleccionesQatar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Qatar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alemania&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Dinamarca&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Brasil&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Francia&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                          &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Paises Bajos&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Argentina&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Iran&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Corea del Sur&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Japon&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Arabia Saudita&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                          &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ecuador&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Uruguay&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Canada&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ghana&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Senegal&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Marruecos&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Tunez&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Portugal&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                          &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Polonia&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Camerun&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Mexico&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Estados Unidos&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Gales&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Australia&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Costa Rica&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Queremos averiguar cuáles de estas selecciones han sido campeonas del mundo y cuántas veces.&lt;/p&gt;

&lt;h3&gt;
  
  
  Código
&lt;/h3&gt;

&lt;p&gt;El siguiente código realiza esta tarea:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;listarCampeones&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;seleccionesQatar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;seleccion&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;seleccionesQatar&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;campeonesMundiales&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;seleccion&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;campeonesMundiales&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`La selección &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;seleccion&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;no ha salido campeona del mundo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`ha ganado &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; mundial(es)`&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;En este código, &lt;strong&gt;por cada selección que participa en Qatar&lt;/strong&gt;, verificamos &lt;strong&gt;cada entrada&lt;/strong&gt; en el arreglo de campeones del mundo. Esto implica dos bucles anidados, lo que resulta en una complejidad cuadrática.&lt;/p&gt;

&lt;h3&gt;
  
  
  Complejidad Matemática
&lt;/h3&gt;

&lt;p&gt;Cada bucle tiene una complejidad lineal &lt;strong&gt;O(n)&lt;/strong&gt;. Al combinarlos, obtenemos:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f(x) = x * x = x²
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Esto significa que el tiempo de ejecución crece proporcionalmente al cuadrado del tamaño de los arreglos. Cuanto más grandes sean los arreglos, más tiempo llevará procesar todas las combinaciones.&lt;/p&gt;




&lt;h2&gt;
  
  
  Expresiones Cúbicas y Potencias Mayores
&lt;/h2&gt;

&lt;p&gt;Si añadimos más bucles anidados al algoritmo, seguimos multiplicando la complejidad lineal. Por ejemplo:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tres bucles anidados:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f(x) = x * x * x = x³
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cuatro bucles anidados:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f(x) = x * x * x * x = x⁴
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Representación Gráfica
&lt;/h3&gt;

&lt;p&gt;A continuación, una gráfica que muestra cómo el tiempo de ejecución se incrementa con el tamaño de los datos en funciones cuadráticas y de mayor potencia:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31t4wg54d6u6f9lyluzt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31t4wg54d6u6f9lyluzt.png" alt="Image description" width="800" height="476"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Como se observa, las potencias más altas hacen que el tiempo de procesamiento crezca de manera exponencial, lo que puede llevar a tiempos de ejecución inaceptables.&lt;/p&gt;




&lt;h2&gt;
  
  
  Recomendaciones
&lt;/h2&gt;

&lt;p&gt;Para optimizar algoritmos con bucles anidados:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Reducir bucles innecesarios:&lt;/strong&gt; Evita iteraciones redundantes que puedan ser optimizadas.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Usar estructuras de datos eficientes:&lt;/strong&gt; Por ejemplo, emplear tablas hash puede reducir búsquedas lineales o cuadráticas a búsquedas constantes (&lt;strong&gt;O(1)&lt;/strong&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dividir y conquistar:&lt;/strong&gt; Algoritmos como quicksort y mergesort usan este enfoque para reducir la complejidad.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Conclusión
&lt;/h2&gt;

&lt;p&gt;En este post, exploramos las expresiones cuadráticas y el impacto de las potencias mayores en la complejidad computacional. Estas notaciones son esenciales para identificar problemas de escalabilidad en algoritmos.&lt;/p&gt;

&lt;p&gt;En la próxima parte, hablaremos de otros tipos de expresiones, como las &lt;strong&gt;logarítmicas&lt;/strong&gt;, &lt;strong&gt;exponenciales&lt;/strong&gt; y &lt;strong&gt;factoriales&lt;/strong&gt;. ¡No te lo pierdas!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Big-O en Español - Parte 1</title>
      <dc:creator>Martin Pi</dc:creator>
      <pubDate>Mon, 06 Jan 2025 14:20:43 +0000</pubDate>
      <link>https://dev.to/martin_pi/big-o-en-espanol-parte-1-3d8</link>
      <guid>https://dev.to/martin_pi/big-o-en-espanol-parte-1-3d8</guid>
      <description>&lt;p&gt;La notación Big-O es una herramienta fundamental en el desarrollo de software, ya que nos permite analizar y comprender la eficiencia de los algoritmos que implementamos. Más allá de ser un concepto teórico, Big-O juega un papel crucial al momento de optimizar tiempos de procesamiento y garantizar la escalabilidad de nuestras aplicaciones.&lt;/p&gt;

&lt;p&gt;Esta serie de posts nace de conversaciones con clientes, donde frecuentemente trabajo en optimizar no solo arquitecturas, sino también los tiempos de respuesta de funcionalidades críticas. Comprender Big-O no solo mejora el rendimiento de nuestros sistemas, sino que también nos ayuda a entender conceptos como la &lt;strong&gt;complejidad ciclomatica&lt;/strong&gt; y a tomar decisiones más informadas en el diseño de soluciones.&lt;/p&gt;

&lt;p&gt;A través de esta serie, exploraremos desde los conceptos más básicos hasta los casos avanzados, proporcionando ejemplos prácticos que te ayudarán a aplicar estas ideas en tus proyectos.&lt;/p&gt;




&lt;h2&gt;
  
  
  ¿Qué encontrarás en esta serie?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/martin_pi/big-o-en-espanol-parte-3-4j17#"&gt;Parte 1: Expresiones Lineales y Constantes&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Introducción a las notaciones más simples y cómo afectan el rendimiento de nuestros algoritmos.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/martin_pi/big-o-en-espanol-parte-2-4kpk"&gt;Parte 2: Expresiones Cuadráticas, Cúbicas y de Otras Potencias&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Un análisis más profundo sobre cómo crecen los tiempos de ejecución en algoritmos más complejos.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/martin_pi/big-o-en-espanol-parte-3-4j17"&gt;Parte 3: Expresiones Logarítmicas, Exponenciales y Factoriales&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Exploraremos casos donde el tiempo de ejecución crece de forma mucho más drástica.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/martin_pi/big-o-en-espanol-parte-4-58ff"&gt;Parte 4: Combinando Complejidades y Comparando Estructuras de Datos&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Analizaremos cómo diferentes estructuras de datos afectan la complejidad y cómo combinar varios algoritmos.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/martin_pi/big-o-en-espanol-parte-5-455e"&gt;Parte 5: Consideraciones Avanzadas y Casos Reales&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Reflexiones finales sobre trade-offs entre tiempo y espacio, complejidades amortizadas, y cómo aplicar Big-O en escenarios reales.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  ¿Por qué es importante?
&lt;/h2&gt;

&lt;p&gt;En un mundo donde la experiencia del usuario y la escalabilidad son aspectos esenciales, optimizar el rendimiento no es opcional, es un imperativo. La notación Big-O nos da una base sólida para:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Identificar puntos de mejora en nuestros algoritmos.&lt;/li&gt;
&lt;li&gt;Evaluar el impacto de decisiones arquitectónicas.&lt;/li&gt;
&lt;li&gt;Garantizar que nuestras aplicaciones sean escalables a medida que crecen los datos.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Te invito a sumarte a esta serie y profundizar en uno de los conceptos más importantes en el desarrollo de software moderno.&lt;/p&gt;




&lt;p&gt;En esta primera parte, exploraremos dos de las notaciones más comunes: &lt;strong&gt;O(n)&lt;/strong&gt; (lineal) y &lt;strong&gt;O(1)&lt;/strong&gt; (constante), utilizando ejemplos prácticos.&lt;/p&gt;




&lt;h2&gt;
  
  
  Expresiones Lineales
&lt;/h2&gt;

&lt;p&gt;Imagina que tienes una lista de personas en un arreglo desordenado:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;personas&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Juan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Pedro&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Pablo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Damian&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Maria&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Diego&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Nelson&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jose&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Monica&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Juana&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Si quieres buscar a una persona específica en la lista, necesitas recorrer cada elemento uno por uno hasta encontrarla. Si la persona no está en la lista, tendrás que revisar todos los elementos.&lt;/p&gt;

&lt;p&gt;El siguiente código ilustra este proceso:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;encontrarPersona&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nombre&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;persona&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;personas&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;persona&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;nombre&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Persona Encontrada&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Persona No Encontrada&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;En este caso:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Mejor caso:&lt;/strong&gt; La persona está en la primera posición (O(1)).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Peor caso:&lt;/strong&gt; La persona no está o está al final de la lista (O(n)).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caso promedio:&lt;/strong&gt; Se recorre la mitad de los elementos, pero en Big-O se simplifica como O(n).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;En matemáticas, esta relación se expresa como:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f(x) = x
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;En notación Big-O, se representa así:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;O(n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Esto significa que, a medida que el número de elementos en el arreglo crece, el tiempo necesario para encontrar una persona también crece linealmente.&lt;/p&gt;




&lt;h2&gt;
  
  
  Expresiones Constantes
&lt;/h2&gt;

&lt;p&gt;Ahora supongamos que tienes una tabla hash (hashtable). Este tipo de estructura permite recuperar valores en base a una clave de forma casi instantánea.&lt;/p&gt;

&lt;p&gt;Ejemplo de tabla hash:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;colores&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Blanco&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#FFFFFF&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Rojo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#FF0000&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Negro&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#000000&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Amarillo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#FFFF00&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Verde&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#008000&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cuando intentas recuperar un valor por su clave, como en los siguientes ejemplos:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;colorBlanco&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;colores&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Blanco&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// Resultado: "#FFFFFF"&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;colorVerde&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;colores&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Verde&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;   &lt;span class="c1"&gt;// Resultado: "#008000"&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;colorAzul&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;colores&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Azul&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;    &lt;span class="c1"&gt;// Resultado: undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;El tiempo necesario para acceder a cualquier valor es constante, independientemente de cuántos elementos haya en la tabla hash.&lt;/p&gt;

&lt;p&gt;En matemáticas, esta relación se expresa como:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f(x) = c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Y en notación Big-O:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;O(1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Esto significa que el tiempo de ejecución no depende del tamaño de la estructura de datos.&lt;/p&gt;




&lt;p&gt;Las gráficas de estas dos notaciones se pueden ver a continuación&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbn1xrilo8mb51q2vzigm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbn1xrilo8mb51q2vzigm.png" alt="Image description" width="531" height="461"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Respondiendo Preguntas Comunes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  ¿Qué pasa si mi procesador es antiguo o muy rápido?
&lt;/h3&gt;

&lt;p&gt;La notación Big-O mide la &lt;strong&gt;tendencia&lt;/strong&gt; del tiempo de ejecución en relación con el tamaño de los datos, no el tiempo exacto. Por ejemplo:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Si el procesador es más lento, podrías tener:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  f(x) = 2x
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Si es más rápido:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  f(x) = x / 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Si hay un retraso inicial antes de ejecutar:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  f(x) = x + 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sin embargo, en todos estos casos, la tendencia sigue siendo la misma: &lt;strong&gt;f(x) = x&lt;/strong&gt; (lineal).&lt;/p&gt;

&lt;p&gt;Lo mismo aplica para O(1), donde el tiempo puede variar, pero sigue siendo constante.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusión
&lt;/h2&gt;

&lt;p&gt;En este post, hemos explorado las dos notaciones más simples de Big-O: &lt;strong&gt;O(n)&lt;/strong&gt; (lineal) y &lt;strong&gt;O(1)&lt;/strong&gt; (constante). Estas son esenciales para entender cómo los algoritmos escalan con datos más grandes.&lt;/p&gt;

&lt;p&gt;En la próxima parte, hablaremos de &lt;strong&gt;expresiones cuadráticas&lt;/strong&gt; y otros casos más complejos. ¡Prepárate para llevar tu comprensión de Big-O al siguiente nivel!&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>datastructures</category>
      <category>beginners</category>
      <category>developer</category>
    </item>
    <item>
      <title>Azure Logs Analytics for CosmosDB</title>
      <dc:creator>Martin Pi</dc:creator>
      <pubDate>Mon, 30 Dec 2024 13:34:37 +0000</pubDate>
      <link>https://dev.to/martin_pi/azure-logs-analytics-for-cosmosdb-4284</link>
      <guid>https://dev.to/martin_pi/azure-logs-analytics-for-cosmosdb-4284</guid>
      <description>&lt;p&gt;Azure CosmosDB allows you to enable and store logs in Log Analytics, which is helpful for troubleshooting and performance analysis.&lt;/p&gt;




&lt;h2&gt;
  
  
  Enabling Log Analytics on CosmosDB
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Access the CosmosDB account and, in the left panel, go to &lt;strong&gt;Monitoring &amp;gt; Diagnostic Settings&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmkhul06wtfz1tyxsoz5n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmkhul06wtfz1tyxsoz5n.png" alt="Image description" width="311" height="356"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A message will prompt you about enabling "Full Text Query." Click &lt;strong&gt;Not Now&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwnazbcveffv6yhi54cwa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwnazbcveffv6yhi54cwa.png" alt="Image description" width="800" height="350"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If no Diagnostic Settings are enabled, click &lt;strong&gt;Add Diagnostic Settings&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkimomwjaev9t0ypxawc9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkimomwjaev9t0ypxawc9.png" alt="Image description" width="800" height="345"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Select the logs you want to store in Log Analytics.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F34yt7qqaycc9yv98v870.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F34yt7qqaycc9yv98v870.png" alt="Image description" width="800" height="536"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Categories of Logs
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;Category&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;DataPlaneRequest&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Captures every data plane operation (e.g., create, update, delete, retrieve) for the CosmosDB account.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;QueryRuntimeStatistics&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Details query operations executed against a SQL API account, with optional full-text query logging.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;PartitionKeyStatistics&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Provides insights into logical partition keys with disproportionate storage utilization.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;PartitionKeyRUConsumption&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Tracks RU consumption for logical partition keys in each region and identifies hot partitions.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;ControlPlaneRequest&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Logs all control plane operations, including indexing policy updates, backups, and account modifications.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; While enabling logs is useful, it can incur additional costs in the Log Analytics workspace depending on database usage. Regularly monitor log consumption.&lt;/p&gt;

&lt;h3&gt;
  
  
  Checking Log Volume
&lt;/h3&gt;

&lt;p&gt;Use the following KQL query to verify log volume:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AzureDiagnostics
| where ResourceProvider == "MICROSOFT.DOCUMENTDB"
| summarize count() by Category
| order by count_ desc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk62ke0fbyk2ldze4zykh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk62ke0fbyk2ldze4zykh.png" alt="Image description" width="366" height="292"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For example, in a 24-hour period, over 112 million logs were stored in the &lt;strong&gt;DataPlaneRequest&lt;/strong&gt; category.&lt;/p&gt;




&lt;h2&gt;
  
  
  Accessing Logs in CosmosDB
&lt;/h2&gt;

&lt;p&gt;To access logs, navigate to &lt;strong&gt;Monitoring &amp;gt; Logs&lt;/strong&gt; in the CosmosDB account.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzty7lrjwkiwc2blmboka.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzty7lrjwkiwc2blmboka.png" alt="Image description" width="311" height="356"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Log Analytics provides pre-defined queries separated into &lt;strong&gt;Alerts&lt;/strong&gt; and &lt;strong&gt;Diagnostics&lt;/strong&gt;:&lt;/p&gt;

&lt;h3&gt;
  
  
  Alerts Queries
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftfkchblzpn7wvvkddcrt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftfkchblzpn7wvvkddcrt.png" alt="Image description" width="800" height="430"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Diagnostics Queries
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsnkvg2k8hohei1oh7yz5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsnkvg2k8hohei1oh7yz5.png" alt="Image description" width="800" height="441"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Sample Query
&lt;/h3&gt;

&lt;p&gt;The following query identifies top operations by consumed RUs in the last 24 hours:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Top operations by consumed Request Units (RUs) in last 24 hours
AzureDiagnostics
| where TimeGenerated &amp;gt;= ago(24h)
| where Category == "DataPlaneRequests"
| summarize numberOfOperations = count(), totalConsumedRU = sum(todouble(requestCharge_s)) by databaseName_s, collectionName_s, OperationName, requestResourceType_s, requestResourceId_s, _ResourceId
| extend averageRUPerOperation = totalConsumedRU / numberOfOperations
| order by numberOfOperations
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feju6e3pt23mwzwwp2t85.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feju6e3pt23mwzwwp2t85.png" alt="Image description" width="434" height="208"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Available Logs in CosmosDB
&lt;/h3&gt;

&lt;p&gt;On the left panel, you can view logs available for CosmosDB:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flh5lok5kg4c2rbvuyftz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flh5lok5kg4c2rbvuyftz.png" alt="Image description" width="356" height="274"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Enabling Full Text Query
&lt;/h2&gt;

&lt;p&gt;By default, CosmosDB does not enable full-text query logging. Enabling this feature improves query visibility.&lt;/p&gt;

&lt;h3&gt;
  
  
  Comparison
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Default Logs (without full-text query):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;p3&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;"123"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Logs with Full Text Query Enabled:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;firstname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lastname&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;"123"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How to Enable Full Text Query
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to &lt;strong&gt;Diagnostic Settings&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Go to Feature&lt;/strong&gt; when prompted to enable full-text query logging.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6vdzau2ey1dlh5gcgwmz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6vdzau2ey1dlh5gcgwmz.png" alt="Image description" width="800" height="350"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Click &lt;strong&gt;Diagnostics full-text query&lt;/strong&gt; and enable it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkmm9f86d92qfgh2wnz3v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkmm9f86d92qfgh2wnz3v.png" alt="Image description" width="800" height="174"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Log Analytics is a powerful tool for monitoring and troubleshooting CosmosDB. Enabling appropriate logs can help identify issues like hot partitions, query bottlenecks, and resource utilization patterns. However, it is crucial to balance the logs collected with the cost implications in Log Analytics. Using targeted queries and insights will maximize efficiency while keeping costs under control.&lt;/p&gt;

&lt;p&gt;Future posts will delve into advanced troubleshooting techniques for CosmosDB.&lt;/p&gt;




&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/azure-monitor/logs/log-analytics-overview" rel="noopener noreferrer"&gt;Log Analytics Overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/azure-monitor/logs/log-analytics-tutorial" rel="noopener noreferrer"&gt;Log Analytics Tutorial&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/" rel="noopener noreferrer"&gt;Kusto Query Language (KQL)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/cosmos-db/logging" rel="noopener noreferrer"&gt;Azure CosmosDB Diagnostic Logging&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>azure</category>
      <category>monitoring</category>
      <category>architecture</category>
      <category>nosql</category>
    </item>
    <item>
      <title>Part 2 - CosmosDB Logical Partition and the Impact on Partition Key Choice</title>
      <dc:creator>Martin Pi</dc:creator>
      <pubDate>Mon, 23 Dec 2024 15:34:36 +0000</pubDate>
      <link>https://dev.to/martin_pi/part-2-cosmosdb-logical-partition-and-the-impact-on-partition-key-choice-5oe</link>
      <guid>https://dev.to/martin_pi/part-2-cosmosdb-logical-partition-and-the-impact-on-partition-key-choice-5oe</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm00viys9gaaoo9k1dxmt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm00viys9gaaoo9k1dxmt.png" alt="Image description" width="343" height="316"&gt;&lt;/a&gt;In the first &lt;a href="https://dev.to/martin_pi/partitions-in-azure-cosmos-db-a-common-discussion-with-customers-2n66"&gt;part&lt;/a&gt; of this series, I explained physical partitions, their theoretical maximum, reasons for creating multiple partitions, RU distribution, and key metrics.&lt;/p&gt;

&lt;p&gt;In this post, the focus will be on &lt;strong&gt;Logical Partitions&lt;/strong&gt;, addressing common questions such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is a Logical Partition?&lt;/li&gt;
&lt;li&gt;What is a Partition Key?&lt;/li&gt;
&lt;li&gt;What is the Max Size?&lt;/li&gt;
&lt;li&gt;What is the impact on Physical Partitions?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s explore these concepts with a real-time streaming scenario.&lt;/p&gt;




&lt;h2&gt;
  
  
  Scenario
&lt;/h2&gt;

&lt;p&gt;Suppose an energy company (Oil and Gas) has millions of sensors streaming data to local data loggers. These loggers, located in different geographical areas, stream data to a centralized Kafka or IoT Hub. A service listens to this data, processes it, and stores it in CosmosDB.&lt;/p&gt;

&lt;p&gt;Each message in Kafka or IoT Hub is in JSON format, as shown below:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;deviceId&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Sensor or Device ID&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;deviceName&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Sensor or Device Name&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;dataLoggerId&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Data Logger transmitting the data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;plantId&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Plant where the Data Logger is connected&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;plantName&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Friendly name of the Plant&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;time&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;UTC time of the telemetry in ISO 8601 format&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ticks&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Ticks when the telemetry was generated&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;epoch&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Epoch time (seconds) of the telemetry&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;location&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Latitude, Longitude, and Accuracy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;device-data&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Type, Measure Unit, Measure Value&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;JSON Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"deviceId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"a7d96e7e-16ae-400e-a616-e84dba0d8633"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"deviceName"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"TEMP-ALBA-01"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"dataLoggerId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"0f472328-7214-4bbd-9789-50519f44bb23"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"plantId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"PLANT-1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"plantName"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"North-Alba-01"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"time"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2022-07-19T10:10:11.5091350Z"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"ticks"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;637097550115091350&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"epoch"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1658251025&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"deviceData"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"temp-sensor"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"measureUnitDescription"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"degrees Celsius"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"measureUnit"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"C"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"measureValue"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"location"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"latitude"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"35.063375"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"accuracy"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"longitude"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"-89.963738"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Scenario Details
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Messages per second:&lt;/strong&gt; ~10,000&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Message size:&lt;/strong&gt; 1–2 KB&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data retention:&lt;/strong&gt; 30 days (TTL: 30 days)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Estimated storage:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;~70 GB/day&lt;/li&gt;
&lt;li&gt;~2 TB/month&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  What is a Logical Partition?
&lt;/h2&gt;

&lt;p&gt;A logical partition consists of items sharing the same partition key. The &lt;strong&gt;maximum size&lt;/strong&gt; for a logical partition is &lt;strong&gt;20 GB&lt;/strong&gt; (subject to change).&lt;/p&gt;




&lt;h2&gt;
  
  
  What is a Partition Key?
&lt;/h2&gt;

&lt;p&gt;A partition key has two components:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Partition Key Path&lt;/strong&gt;: The property in the JSON document used as the key.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Partition Key Value&lt;/strong&gt;: The value associated with the key path.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Partition keys can be &lt;strong&gt;strings&lt;/strong&gt; or &lt;strong&gt;numeric types&lt;/strong&gt; and support alphanumeric and underscore characters (&lt;code&gt;_&lt;/code&gt;). Nested objects can use standard path notation (&lt;code&gt;/&lt;/code&gt;).&lt;/p&gt;




&lt;h2&gt;
  
  
  How Can We Decide the Partition Key in This Scenario?
&lt;/h2&gt;

&lt;p&gt;Given the write-intensive nature of this scenario, careful partition key selection is crucial. Assume the company operates &lt;strong&gt;15 plants&lt;/strong&gt;, with one plant generating &lt;strong&gt;25%&lt;/strong&gt; of the total traffic.&lt;/p&gt;

&lt;p&gt;Additionally, an application consuming CosmosDB data allows users to filter by &lt;strong&gt;plant&lt;/strong&gt;, &lt;strong&gt;data logger&lt;/strong&gt;, or &lt;strong&gt;sensor&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Alternative 1:&lt;/strong&gt; Using &lt;code&gt;plantId&lt;/code&gt; as the Partition Key
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"plantId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"PLANT-1"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Advantages:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Groups all data for a plant within the same logical partition, avoiding cross-partition queries.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Disadvantages:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Limited logical partitions (only 15, one for each plant).&lt;/li&gt;
&lt;li&gt;Maximum logical partition size is 20 GB. With 15 plants, the total capacity is 300 GB, far below the 2 TB needed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt; &lt;code&gt;plantId&lt;/code&gt; is not a suitable partition key.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Alternative 2:&lt;/strong&gt; Using &lt;code&gt;plantId + CurrentDate&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"partitionKey"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"PLANT-1-20220819"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this approach, data is still grouped by Plant ID, allowing us to query all records for the current day or prior days. This structure is advantageous as it helps avoid cross-partition queries, improving query efficiency.&lt;/p&gt;

&lt;p&gt;Now, let’s analyze the partition size. Based on the scenario, the storage requirement is approximately 70 GB per day, with one plant contributing 25% of the total traffic. This means a single plant will generate around 17.5 GB of data per day.&lt;/p&gt;

&lt;p&gt;Since 17.5 GB &amp;lt; 20 GB, the size of a logical partition stays within the allowed limit. However, let’s examine the impact this design has on physical partitioning behavior.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcy0wzeihgkk22ytj1j7d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcy0wzeihgkk22ytj1j7d.png" alt="Image description" width="800" height="342"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The image highlights two physical partitions that are fully utilized (marked in red) due to the presence of a large logical partition. If additional data continues to be written to this logical partition, the Cosmos DB engine will need to redistribute it by moving some logical partitions to other physical partitions with available space. This rebalancing process is necessary to free up space in the over-utilized physical partition where new data is being added.&lt;/p&gt;

&lt;p&gt;This situation is called Hot partition, and it can be observed on insights metrics for Cosmos DB&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpvzqp1d9udgk5r9khphn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpvzqp1d9udgk5r9khphn.png" alt="Image description" width="800" height="241"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftarjvnj3y178fs6mhlct.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftarjvnj3y178fs6mhlct.png" alt="Image description" width="800" height="140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary of this scenario
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Advantages:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Groups data by plant and day, reducing the size of each logical partition.&lt;/li&gt;
&lt;li&gt;Avoids cross-partition queries for daily queries.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Disadvantages:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Potential for &lt;strong&gt;hot partitions&lt;/strong&gt;: If one plant generates significant traffic, logical partitions may fill up, triggering repartitioning.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt; This is better than &lt;code&gt;plantId&lt;/code&gt; but not ideal.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is the Right Partition Key?
&lt;/h2&gt;

&lt;p&gt;For heavy-write scenarios, prioritize small logical partitions for faster insertion. A &lt;strong&gt;UUID&lt;/strong&gt; or a more granular composite key (e.g., &lt;code&gt;plantId + epoch&lt;/code&gt;) is preferable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Recommendations:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Custom Indexing:&lt;/strong&gt; Define a custom indexing policy instead of indexing all properties.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Monitor Metrics:&lt;/strong&gt; Use Log Analytics to track partition size.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Analyzing Partition Key Size
&lt;/h2&gt;

&lt;p&gt;Microsoft provides a Log Analytics query to analyze partition key sizes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AzureDiagnostics
| where Category == "PartitionKeyStatistics"
| summarize arg_max(TimeGenerated, *) by databaseName_s, collectionName_s, partitionKey_s, _ResourceId
| extend utilizationOf20GBLogicalPartition = sizeKb_d / 20000000
| project TimeGenerated, databaseName_s, collectionName_s, partitionKey_s, sizeKb_d, utilizationOf20GBLogicalPartition, _ResourceId
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Sample Output:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;sizeKb_d&lt;/code&gt;: Logical partition size (e.g., 6.26 GB).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;utilizationOf20GBLogicalPartition&lt;/code&gt;: Utilization percentage (e.g., 30%).&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Choosing the right partition key is critical for optimal performance in Azure CosmosDB. For write-intensive scenarios like this, selecting a key that evenly distributes data across logical partitions is essential to prevent hot partitions and ensure scalability. Proper monitoring and indexing strategies can further enhance performance and reduce costs.&lt;/p&gt;




&lt;h2&gt;
  
  
  Official References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/cosmos-db/partitioning-overview" rel="noopener noreferrer"&gt;Partitioning and Horizontal Scaling in Azure Cosmos DB&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://microsoft.github.io/AzureTipsAndTricks/blog/tip335.html" rel="noopener noreferrer"&gt;How to Choose a Partition Key in Azure Cosmos DB&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/cosmos-db/best-practices" rel="noopener noreferrer"&gt;Azure Cosmos DB: Best Practices&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>azure</category>
      <category>database</category>
      <category>nosql</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Partitions in Azure Cosmos DB: A Common Discussion with Customers</title>
      <dc:creator>Martin Pi</dc:creator>
      <pubDate>Fri, 20 Dec 2024 20:19:55 +0000</pubDate>
      <link>https://dev.to/martin_pi/partitions-in-azure-cosmos-db-a-common-discussion-with-customers-2n66</link>
      <guid>https://dev.to/martin_pi/partitions-in-azure-cosmos-db-a-common-discussion-with-customers-2n66</guid>
      <description>&lt;p&gt;Partitions in Azure Cosmos DB are a topic I discuss frequently with my customers—at least twice a month! It's a fundamental concept that often raises important questions, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What are physical partitions, logical partitions, and documents?&lt;/li&gt;
&lt;li&gt;When is a physical partition created?&lt;/li&gt;
&lt;li&gt;How can we determine how many physical partitions are in use?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this post, I’ll address these questions specifically for the Cosmos DB SQL API, helping you better understand and manage partitions in your applications.&lt;/p&gt;

&lt;h1&gt;
  
  
  What are physical partitions, logical partitions, and documents?
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhsxcjwiu3d49ona4qooi.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhsxcjwiu3d49ona4qooi.jpg" alt="Image description" width="526" height="351"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Physical Partitions
&lt;/h2&gt;

&lt;p&gt;Physical partitions are the underlying storage units that enable Azure Cosmos DB to scale horizontally by adding more storage and throughput capacity. These partitions are fully managed by Azure Cosmos DB, so you don’t have to worry about their internal implementation or maintenance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Logical Partitions
&lt;/h2&gt;

&lt;p&gt;A logical partition is a grouping of documents that share the same partition key. Logical Partitions are mapped to physical partitions and play a key role in distributing data evenly and improving query performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Documents
&lt;/h2&gt;

&lt;p&gt;Documents are the individual data units stored in Cosmos DB, represented in JSON format. They contain both your application data and associated metadata.&lt;/p&gt;

&lt;h2&gt;
  
  
  Maximum Size Limits (as of September 2022)
&lt;/h2&gt;

&lt;p&gt;The following are the maximum size limits in Cosmos DB (subject to change in the future):&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Physical Partition&lt;/strong&gt;: Maximum size of 50 GB.&lt;br&gt;
&lt;strong&gt;Logical Partition&lt;/strong&gt;: Maximum size of 20 GB.&lt;br&gt;
&lt;strong&gt;Document&lt;/strong&gt;: Maximum size of 2 MB.&lt;/p&gt;
&lt;h1&gt;
  
  
  When is a Physical Partition Created?
&lt;/h1&gt;

&lt;p&gt;A new physical partition is created when one of the following thresholds is reached:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Size&lt;/strong&gt;: When the total storage in a physical partition exceeds 50 GB.&lt;br&gt;
&lt;strong&gt;Throughput (RUs)&lt;/strong&gt;: When the provisioned throughput for a single partition exceeds 10,000 request units (RUs).&lt;/p&gt;

&lt;p&gt;These theoretical maximums are not isolated, here are other factors that need to be considered.&lt;/p&gt;

&lt;p&gt;What is the number of RUs defined in our Container or Database? (For this explanation, I will focus exclusively on containers, not databases).&lt;/p&gt;

&lt;p&gt;That information can be checked into the container as shown below&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj3he526stm3waoonpn4w.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj3he526stm3waoonpn4w.jpg" alt="Image description" width="280" height="180"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then in the Scale section we can see the number of RUs assigned to this Container&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyh66y7nxn2q9onnau26o.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyh66y7nxn2q9onnau26o.jpg" alt="Image description" width="749" height="307"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Based on the configuration presented in the image, we can see that the Maximum RUs is 40,000, it is higher than the 10,000 (Theoretical Maximum)&lt;/p&gt;
&lt;h2&gt;
  
  
  40,000 RUs &amp;gt; 10,000 RUs - What Does It Mean?
&lt;/h2&gt;

&lt;p&gt;If your container is provisioned with 40,000 RUs, and each physical partition supports a maximum of 10,000 RUs, Cosmos DB will distribute the provisioned throughput across multiple physical partitions.&lt;/p&gt;
&lt;h2&gt;
  
  
  How Does This Work in Practice?
&lt;/h2&gt;

&lt;p&gt;For example, with 40,000 RUs, Cosmos DB will automatically create 4 physical partitions, each capable of supporting up to 10,000 RUs. This partitioning ensures that the system can handle the throughput demand efficiently.&lt;/p&gt;

&lt;p&gt;In this case the distribution may be something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8lxrj4yg2lntq421yigz.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8lxrj4yg2lntq421yigz.jpg" alt="Image description" width="800" height="398"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Each partition size is less than 50 GB (maximum theorical size) but there are 4 partitions due to the RUs consumption.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In scenarios involving &lt;strong&gt;heavy read operations&lt;/strong&gt;, such as running queries frequently or at high volume, Cosmos DB will create additional physical partitions to distribute the workload. This partitioning is based on the provisioned RUs defined in your container.&lt;/p&gt;
&lt;h2&gt;
  
  
  What Happens in a Heavy Writing Scenario?
&lt;/h2&gt;

&lt;p&gt;In heavy writing scenarios, two critical factors must be considered:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Provisioned RUs&lt;/strong&gt;: The throughput allocated to the container.&lt;br&gt;
&lt;strong&gt;Partition Size&lt;/strong&gt;: The amount of data stored, as physical partitions have a maximum size of 50 GB.&lt;/p&gt;
&lt;h3&gt;
  
  
  Example: Writing Large Data Volumes
&lt;/h3&gt;

&lt;p&gt;Let’s consider a scenario where your container holds 360 GB of data:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Partition Requirement&lt;/strong&gt;: To support this volume, you would need:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Number of Physical Partitions = Total Data Size / Physical Partition Max Size
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Substituting the values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Number of Physical Partitions = 360 GB / 50 GB = 7.2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since partitions must be whole numbers, Cosmos DB will round up to 8 physical partitions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Provisioned RU Distribution&lt;/strong&gt;: If you provision 40,000 RUs, these RUs will be distributed equally across the 8 physical partitions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RUs per Physical Partition = Total RUs / Number of Partitions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Substituting the values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RUs per Physical Partition = 40,000 RUs / 8 = 5,000 RUs per Partition
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Implications for Heavy Writing Workloads
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Even Data Distribution&lt;/strong&gt;: Data written to the container will be distributed across the 8 physical partitions, based on the partition key. Uneven distribution can lead to “hot” partitions, where a few partitions receive a disproportionate number of writes, causing performance bottlenecks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Partition Key Importance&lt;/strong&gt;: Choosing an appropriate partition key is essential in heavy write scenarios. A well-designed key ensures that data and write operations are evenly distributed across all partitions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scaling&lt;/strong&gt;: If either the partition size exceeds 50 GB or the throughput for a single partition exceeds 10,000 RUs, additional physical partitions will be created dynamically.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7k22d4wxub9vrhg23j7t.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7k22d4wxub9vrhg23j7t.jpg" alt="Image description" width="800" height="251"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, the provisioned 40,000 RUs are evenly distributed across the 8 physical partitions, resulting in 5,000 RUs per partition. This ensures that each partition has sufficient throughput to handle the workload.&lt;/p&gt;

&lt;h2&gt;
  
  
  Recent Features Enhancing Partition Management
&lt;/h2&gt;

&lt;p&gt;Azure Cosmos DB has introduced features to further optimize partitioning:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Burst Capacity&lt;/strong&gt;: This feature allows containers and databases to handle unexpected traffic spikes by utilizing unused throughput, enhancing performance during peak times.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Materialized Views&lt;/strong&gt;: Materialized views enable precomputed views of your data, improving query performance by reducing the need for on-the-fly computations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices for Partition Management
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Monitor Partition Sizes&lt;/strong&gt;: Regularly check the size of your logical partitions to ensure they remain within the 20 GB limit. Azure Monitor can be configured to alert you when a partition approaches this threshold.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Data Modeling Strategies&lt;/strong&gt;: Design your data model to align with your partitioning strategy. For example, in a multi-tenant application, using tenant IDs as partition keys can help segregate and manage data efficiently.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Avoid Frequent Updates to Partition Key Values&lt;/strong&gt;: Since partition keys are immutable, design your data model to minimize the need for updates to these values. If a change is necessary, you'll need to create a new item with the desired partition key and delete the old item.&lt;/p&gt;

&lt;h1&gt;
  
  
  How Do We Know How Many Physical Partitions Are Being Created?
&lt;/h1&gt;

&lt;p&gt;This is one of the most common questions I receive, and it's essential to understand how to monitor your Cosmos DB partitioning.&lt;/p&gt;

&lt;p&gt;Azure Cosmos DB provides built-in dashboards to help you visualize this information. You can find it under:&lt;/p&gt;

&lt;p&gt;Monitoring → Insights&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1cuykmr3i9jf2fqpet5f.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1cuykmr3i9jf2fqpet5f.jpg" alt="Image description" width="207" height="168"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From there, you can view details about the number of physical partitions created, along with other metrics like throughput usage, partition key distribution, and RU consumption. These insights allow you to track partitioning behavior and make informed decisions about scaling and optimization.&lt;/p&gt;

&lt;p&gt;Under throughput option in (image below)&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffb8546h7h1tm3aq0bhc9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffb8546h7h1tm3aq0bhc9.jpg" alt="Image description" width="735" height="74"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is a chart called Normalized RU Consumption, there you can see the number of partitions. In this case 2 partitions&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj73zvc54hgs2otz9bwuv.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj73zvc54hgs2otz9bwuv.jpg" alt="Image description" width="800" height="149"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Effective partitioning in Azure Cosmos DB is essential for building scalable and high-performance applications. By selecting appropriate partition keys, leveraging new features like burst capacity and materialized views, and adhering to best practices, you can optimize your database's performance and scalability.&lt;/p&gt;

&lt;h1&gt;
  
  
  What's Next?
&lt;/h1&gt;

&lt;p&gt;In the next post of this series, I will dive deeper into Logical Partitions and explore how specific scenarios can impact the behavior and creation of physical partitions. Stay tuned for actionable insights and best practices!&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/cosmos-db/partitioning-overview" rel="noopener noreferrer"&gt;Azure Cosmos DB Partitioning Overview&lt;/a&gt;&lt;br&gt;&lt;br&gt;
Comprehensive guide on partitioning in Azure Cosmos DB, including logical and physical partitions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/cosmos-db/sql/sql-api-introduction" rel="noopener noreferrer"&gt;Azure Cosmos DB SQL API Documentation&lt;/a&gt;&lt;br&gt;&lt;br&gt;
Official documentation for working with the SQL API in Cosmos DB.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/cosmos-db/partition-key-best-practices" rel="noopener noreferrer"&gt;Best Practices for Partitioning in Azure Cosmos DB&lt;/a&gt;&lt;br&gt;&lt;br&gt;
Tips and strategies for selecting partition keys and managing partitions effectively.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/cosmos-db/monitor-resource" rel="noopener noreferrer"&gt;Azure Cosmos DB Insights - Monitoring and Metrics&lt;/a&gt;&lt;br&gt;&lt;br&gt;
Explanation of how to use the Monitoring → Insights dashboard to track partition behavior and other metrics.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/cosmos-db/request-units" rel="noopener noreferrer"&gt;Azure Cosmos DB Request Units (RUs)&lt;/a&gt;&lt;br&gt;&lt;br&gt;
Detailed information on RUs, their usage, and how they impact partitioning and performance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/cosmos-db/faq" rel="noopener noreferrer"&gt;Azure Cosmos DB FAQ - Scaling and Partitioning&lt;/a&gt;&lt;br&gt;&lt;br&gt;
Frequently asked questions related to scaling, partitions, and throughput in Cosmos DB.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>azure</category>
      <category>database</category>
      <category>nosql</category>
      <category>architecture</category>
    </item>
    <item>
      <title>DevContainers: Big and Bold or Small and Smart?</title>
      <dc:creator>Martin Pi</dc:creator>
      <pubDate>Tue, 10 Dec 2024 19:07:53 +0000</pubDate>
      <link>https://dev.to/martin_pi/devcontainers-big-and-bold-or-small-and-smart-1i61</link>
      <guid>https://dev.to/martin_pi/devcontainers-big-and-bold-or-small-and-smart-1i61</guid>
      <description>&lt;p&gt;Recently, I was deep into a project juggling multiple technologies—think of it like trying to cook a gourmet meal with ingredients from five different cuisines. Then, the ultimate question from the customer dropped:&lt;/p&gt;

&lt;p&gt;👉 "Should we go with one mega DevContainer to rule them all or split things up into individual containers?"&lt;/p&gt;

&lt;p&gt;It sounded simple at first, but as I dove into the details, it became one of those "hold my coffee, let me investigate" moments. After some hands-on exploration, here’s what I found—and it might just save you some dev-time headaches too!&lt;/p&gt;

&lt;p&gt;When dealing with a solution containing multiple technologies like React with TypeScript, .NET 9, and Java, the best approach to organizing your Dev Containers depends on your team setup, project requirements, and the level of independence needed for each component.&lt;/p&gt;

&lt;h1&gt;
  
  
  Options: Single vs. Multiple Dev Containers
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Option 1: Single Dev Container
&lt;/h2&gt;

&lt;p&gt;A single Dev Container includes everything needed for all technologies.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unified environment: All team members have the same tools and dependencies.&lt;/li&gt;
&lt;li&gt;Simplified setup: Only one container to manage.&lt;/li&gt;
&lt;li&gt;Seamless cross-component testing: Easier to run React, .NET, and Java services together for integration testing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Larger image size: The container might become bloated with unnecessary dependencies.&lt;/li&gt;
&lt;li&gt;Complex configuration: Combining dependencies for multiple runtimes (Node.js, .NET, Java) in one container can be tricky.&lt;/li&gt;
&lt;li&gt;Overhead for non-full-stack devs: Developers focusing on only one part (e.g., frontend) might have unnecessary tools installed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Recommended When:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All team members need to work across all components.&lt;/li&gt;
&lt;li&gt;Your projects are tightly coupled and require frequent integration.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example devcontainer.json (Single Dev Container):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "Full Stack Environment",
  "build": {
    "dockerfile": "Dockerfile"
  },
  "settings": {
    "terminal.integrated.defaultProfile.linux": "bash"
  },
  "extensions": [
    "dbaeumer.vscode-eslint",
    "ms-dotnettools.csharp",
    "redhat.java",
    "esbenp.prettier-vscode"
  ],
  "postCreateCommand": "npm install &amp;amp;&amp;amp; dotnet restore &amp;amp;&amp;amp; ./gradlew build"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example Dockerfile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Base image with Node.js, .NET 9 SDK, and Java 17
FROM mcr.microsoft.com/devcontainers/universal:2

# Install .NET 9 SDK
RUN apt-get update &amp;amp;&amp;amp; apt-get install -y dotnet-sdk-9.0

# Install Java (default: OpenJDK 17)
RUN apt-get update &amp;amp;&amp;amp; apt-get install -y openjdk-17-jdk

# Set up default work directory
WORKDIR /workspaces/project
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Option 2: Multiple Dev Containers
&lt;/h2&gt;

&lt;p&gt;Create separate Dev Containers for each component (React, .NET, Java) and allow developers to work on individual parts independently.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Optimized environments: Each container is lightweight and focused on its technology stack.&lt;/li&gt;
&lt;li&gt;Modular development: Frontend and backend developers can work independently.&lt;/li&gt;
&lt;li&gt;Easier maintenance: Updates and changes to dependencies are isolated.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;More setup time: Multiple Dev Containers require more configuration.&lt;/li&gt;
&lt;li&gt;Cross-component challenges: Running and testing the entire system requires additional steps (e.g., Docker Compose).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Recommended When:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Team members specialize in specific components (frontend vs. backend).&lt;/li&gt;
&lt;li&gt;Your projects are loosely coupled or use APIs to interact.&lt;/li&gt;
&lt;li&gt;Developers often work on just one part at a time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;*&lt;em&gt;Example Setup (Multiple Dev Containers): *&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Frontend (React + TypeScript) devcontainer.json:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "React TypeScript Environment",
  "image": "mcr.microsoft.com/devcontainers/javascript-node:16",
  "settings": {
    "terminal.integrated.defaultProfile.linux": "bash"
  },
  "extensions": [
    "dbaeumer.vscode-eslint",
    "esbenp.prettier-vscode"
  ],
  "postCreateCommand": "npm install"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Backend .NET (API) devcontainer.json:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": ".NET 9 Environment",
  "image": "mcr.microsoft.com/devcontainers/dotnet:9.0",
  "settings": {
    "terminal.integrated.defaultProfile.linux": "bash"
  },
  "extensions": [
    "ms-dotnettools.csharp"
  ],
  "postCreateCommand": "dotnet restore"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Backend Java (API) devcontainer.json:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "Java Development Environment",
  "image": "mcr.microsoft.com/devcontainers/java:17",
  "settings": {
    "terminal.integrated.defaultProfile.linux": "bash"
  },
  "extensions": [
    "redhat.java",
    "vscjava.vscode-java-debug"
  ],
  "postCreateCommand": "./gradlew build"```


}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Best Practice for Cross-Component Development
&lt;/h3&gt;

&lt;p&gt;If you choose multiple containers but need them to work together:&lt;/p&gt;

&lt;p&gt;Use Docker Compose: Create a docker-compose.yml file to orchestrate containers for the React, .NET, and Java components.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

version: '3.8'
services:
  frontend:
    build: ./frontend
    ports:
      - "3000:3000"
  backend-dotnet:
    build: ./backend-dotnet
    ports:
      - "5000:5000"
  backend-java:
    build: ./backend-java
    ports:
      - "8080:8080"


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Important! - Enable Network Communication: Ensure each container can communicate over Docker's default bridge network.&lt;/p&gt;

&lt;h2&gt;
  
  
  Recommendation
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Start with Multiple Dev Containers&lt;/strong&gt;: Unless your team works full-stack across all components, separate Dev Containers provide better modularity and efficiency.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use Docker Compose for Integration&lt;/strong&gt;: Combine multiple containers during integration and testing phases.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Document Everything&lt;/strong&gt;: Clearly document the setup for each Dev Container and how to integrate them.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>devcontainer</category>
      <category>productivity</category>
      <category>development</category>
      <category>vscode</category>
    </item>
  </channel>
</rss>
