Processing and loop memory optimization

1

The code is simple and working perfectly, but I wanted tips on how to optimize it, what would be the best methods to use to allocate less memory, good practices, etc.

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main(string[] args)
    {
        List<int> numeros = new List<int>();
        List<int> pares = new List<int>();
        List<int> impares = new List<int>();

        Random random = new Random();

        for(int i = 0; i < 20; i++)
        {
            int n = random.Next(1, 99);
            numeros.Add(n);
        }

        foreach (int item in numeros)
        {
            if (item % 2 == 0)
                pares.Add(item);
            else
                impares.Add(item);
        }

        Console.WriteLine("Todos os números:");

        int index = 0;

        foreach (int item in numeros)
        {
            if (index == numeros.Count - 1)
                Console.Write(item + "." + "\n");
            else
                Console.Write(item + ", ");

            index++;
        }

        index = 0;

        Console.WriteLine("\n" + "Números pares:");

        foreach (int item in pares)
        {
            if (index == pares.Count - 1)
                Console.Write(item + "." + "\n");
            else
                Console.Write(item + ", ");

            index++;
        }

        Console.WriteLine("\n" + "Números ímpares:");

        index = 0;

        foreach (int item in impares)
        {
            if (index == impares.Count - 1)
                Console.Write(item + "." + "\n");
            else
                Console.Write(item + ", ");

            index++;
        }
    }
}
    
asked by anonymous 12.08.2017 / 15:00

2 answers

1

Just a few changes, it's still possible to do more, but I see no need to complicate. rs

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main(string[] args)
    {
        List<int> numeros = new List<int>();
        List<int> pares = new List<int>();
        List<int> impares = new List<int>();

        Random random = new Random();

        for(int i = 0; i < 20; i++)
        {
            int n = random.Next(1, 99);
            numeros.Add(n);
            if (n % 2 == 0)
                pares.Add(n);
            else
                impares.Add(n);

        }

        Console.WriteLine("Todos os números:");


        for (int i =0; i < numeros.Count; i++)
           Console.Write( numeros[i].ToString() + (i == numeros.Count-1 ? ".\r\n" : ", " ));


        Console.WriteLine("\nNúmeros pares:");

        for (int i =0; i < pares.Count; i++)
           Console.Write( pares[i].ToString() + (i == pares.Count-1 ? ".\r\n" : ", " ));


        Console.WriteLine("\nNúmeros ímpares:");

        for (int i =0; i < impares.Count; i++)
           Console.Write( impares[i].ToString() + (i == impares.Count-1 ? ".\r\n" : ", " ));

    }
}

I put it in .NET Fiddle

    
12.08.2017 / 15:49
1

Optimization can be a lot. It can be by performance, memory consumption, can be by code or even other things to be defined. Optimization has to test. And what counts today may not be worth it tomorrow.

Optimizing this code should not bring much advantage. One of the few performance improvements is to find out if it is a pair with a bit operator that usually runs faster than the rest operator, but the compiler could do an optimization and not change anything. Some things may still be possible, but it depends on tests to prove that they are optimizations.

The rest I can only narrow down and modernize the code. Of course I could delete one or two of the lists, which could occupy half of the memory, but I see no advantages. There is a reduction that probably creates optimization, for example removing a branching (comparison if or ternary) within a loop like I did in the prints.

As it has no requirements, it is difficult to know what to do. Actually maybe I could cut something else.

As the impression of the lists are identical could play in a function to eliminate duplicity of code, but it might not be the intention.

It has other small details that could be different, but it depends on taste.

using System;
using static System.Console;
using System.Collections.Generic;

public class Program {
    public static void Main(string[] args) {
        var numeros = new List<int>();
        var pares = new List<int>();
        var impares = new List<int>();
        var random = new Random();
        for (int i = 0; i < 20; i++) numeros.Add(random.Next(1, 99));
        foreach (int item in numeros) {
            if ((item & 1) == 0) {
                pares.Add(item);
            } else {
                impares.Add(item);
            }
        }
        WriteLine("Todos os números:");
        for (int i = 0; i < numeros.Count - 1; i++) Write(numeros[i] + ", ");
        WriteLine(numeros[numeros.Count - 1] + ".");
        WriteLine("\nNúmeros pares:");
        for (int i = 0; i < pares.Count - 1; i++) Write(pares[i] + ", ");
        WriteLine(pares[pares.Count - 1] + ".");
        WriteLine("\nNúmeros ímpares:");
        for (int i = 0; i < impares.Count - 1; i++) Write(impares[i] + ", ");
        WriteLine(impares[impares.Count - 1] + ".");
    }
}

See running on .NET Fiddle . And no Coding Ground . Also I placed GitHub for future reference .

    
12.08.2017 / 15:47