Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do unnecessary curly braces reduce performance?

After recently encountering this while programming, I have been wondering about this. Below are 2 snippets which are both legal and compile. Specifically, my question is this.. in the second case, do the brackets make the program slower? Also why is this allowed?

1st case:

if (statement)
{
 // do something
}

2nd case:

{
    if (statement)
    {
        // do something
    }
}

Additionally what if I had something like the code below.. Is the runtime the same as just calling function X without any of the curly brackets.

{
  {
    {
      // call function X
    }
  }
}
like image 884
sparta93 Avatar asked Jul 14 '15 16:07

sparta93


People also ask

Can we skip the curly braces?

So we can omit curly braces only there is a single statement under if-else or loop. Here in both of the cases, the Line1 is in the if block but Line2 is not in the if block. So if the condition fails, or it satisfies the Line2 will be executed always.

Do you need curly braces in Java?

Java has a "feature" which allows you to omit curly brackets when writing if statements, for loops, or while loops containing only a single statement. You should never use this feature – always include curly brackets. The reason for this is because omitting curly brackets increases your chances of writing buggy code.

What is the purpose of a set of curly braces?

Different programming languages have various ways to delineate the start and end points of a programming structure, such as a loop, method or conditional statement. For example, Java and C++ are often referred to as curly brace languages because curly braces are used to define the start and end of a code block.

How do you escape curly braces?

To escape curly braces and interpolate a string inside the String. format() method use triple curly braces {{{ }}} . Similarly, you can also use the c# string interpolation feature instead of String. format().


2 Answers

Most of the time it doesn't make any difference - and you should definitely code for readability more than anything else.

However, curly braces can have an effect on performance, in a surprising way, although it's pretty unusual. Consider this code:

using System;
using System.Collections.Generic;

class Test
{
    static void FewerCurlies()
    {
        List<Action> actions = new List<Action>();
        for (int i = 0; i < 100; i++)
        {
            int x;
            if (i % 3 == 0)
            {
                actions.Add(() => x = 10);
            }

            int y;
            if (i % 3 == 1)
            {
                actions.Add(() => y = 10);
            }
        }
    }

    static void MoreCurlies()
    {
        List<Action> actions = new List<Action>();
        for (int i = 0; i < 100; i++)
        {
            {
                int x;
                if (i % 3 == 0)
                {
                    actions.Add(() => x = 10);
                }
            }

            {
                int y;
                if (i % 3 == 1)
                {
                    actions.Add(() => y = 10);
                }
            }
        }
    }
}

The extra braces in MoreCurlies look redundant, right? Not quite... the generated code looks more like this:

using System;
using System.Collections.Generic;

class Test
{
    static void FewerCurlies()
    {
        List<Action> actions = new List<Action>();
        for (int i = 0; i < 100; i++)
        {
            FewerCurliesCapture capture = new FewerCurliesCapture();
            if (i % 3 == 0)
            {
                actions.Add(capture.Method1);
            }

            if (i % 3 == 1)
            {
                actions.Add(capture.Method2);
            }
        }
    }

    static void MoreCurlies()
    {
        List<Action> actions = new List<Action>();
        for (int i = 0; i < 100; i++)
        {
            {
                MoreCurliesCapture1 capture = new MoreCurliesCapture1();
                if (i % 3 == 0)
                {
                    actions.Add(capture.Method);
                }
            }

            {
                MoreCurliesCapture1 capture = new MoreCurliesCapture2();
                if (i % 3 == 1)
                {
                    actions.Add(capture.Method);
                }
            }
        }
    }

    private class FewerCurliesCapture
    {
        public int x;
        public int y;

        public void Method1()
        {
            x = 10;
        }

        public void Method2()
        {
            y = 10;
        }
    }

    private class MoreCurliesCapture1
    {
        public int x;

        public void Method()
        {
            x = 10;
        }
    }

    private class MoreCurliesCapture2
    {
        public int y;

        public void Method()
        {
            y = 10;
        }
    }
}

The differences here are:

  • An instance of the capture class is created in each iteration of the loop in FewerCurlies, even if it's not used
  • Each instance of the capture class used in FewerCurlies contains both variables, even though each delegate will only actually use one of them, whereas in MoreCurlies each capture class only captures a single variable

This is all somewhat implementation-specific, but it shows that redundant-looking curlies can have an impact.

like image 187
Jon Skeet Avatar answered Oct 09 '22 02:10

Jon Skeet


Short answer is "no, they do not reduce performance".

Curly braces are needed for the compiler to determine scope of variables, and to know where the current group of statements end. Once the compiler finished processing, the code with and without the unnecessary curly braces would produce identical output.

Note that this is related to the performance of compiled code, not to the performance of the compiler itself. The compiler will take additional time to compile your code, simply because the raw size of the input is larger. However, in order for this additional time to become measurable, the number of unnecessary braces needs to be rather extreme.

like image 36
Sergey Kalinichenko Avatar answered Oct 09 '22 00:10

Sergey Kalinichenko