HISE Logo Forum
    • Categories
    • Register
    • Login

    Enums

    Scheduled Pinned Locked Moved Scripting
    7 Posts 2 Posters 1.3k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • d.healeyD
      d.healey
      last edited by

      Should I use enums or is it more efficient to use a const var object or reg variable?

      Libre Wave - Freedom respecting instruments and effects
      My Patreon - HISE tutorials
      YouTube Channel - Public HISE tutorials

      1 Reply Last reply Reply Quote 0
      • Christoph HartC
        Christoph Hart
        last edited by

        Use const variables in combination with a namespace. Accessing this is as just as fast as using the actual literal value; everthing else comes with a performance penalty

        namespace Direction
        {
            const var UP = 0;
            const var DOWN = 1;
            const var BOTH = 2;
        };
        
        // Usage:
        switch(d)
        {
           case Direction.UP:
        
        1 Reply Last reply Reply Quote 0
        • d.healeyD
          d.healey
          last edited by

          Yeah I thought there would be a penalty. What about using enums within a namespace? Since we can't nest namespaces.

          Libre Wave - Freedom respecting instruments and effects
          My Patreon - HISE tutorials
          YouTube Channel - Public HISE tutorials

          1 Reply Last reply Reply Quote 0
          • Christoph HartC
            Christoph Hart
            last edited by

            If you're already in a namespace, you can just use const variables - a common rule is to write constant values and enums LIKE_THIS so you don't get confused :)

            1 Reply Last reply Reply Quote 1
            • d.healeyD
              d.healey
              last edited by

              How does const var Direction = {UP:0, DOWN:1, BOTH:2}; compare for efficiency to using individual const vars?

              Libre Wave - Freedom respecting instruments and effects
              My Patreon - HISE tutorials
              YouTube Channel - Public HISE tutorials

              1 Reply Last reply Reply Quote 0
              • Christoph HartC
                Christoph Hart
                last edited by

                You can profile this easily to measure the difference:

                const var DirectionSlow = { "UP": 0, "DOWN": 1, "UP_DOWN": 2};
                
                namespace DirectionFast
                {
                	const var UP = 0;
                	const var DOWN = 1;
                	const var UP_DOWN = 2;
                };
                
                
                reg i = 0;
                
                Console.print("");
                Console.print("Object Access:");
                
                Console.start();
                
                for(i = 0; i < 1000000; i++)
                {
                	// Use many accesses to diminish the impact of the loop logic
                	DirectionSlow.UP_DOWN;
                	DirectionSlow.UP;
                	DirectionSlow.DOWN;
                	DirectionSlow.UP_DOWN;
                	DirectionSlow.UP;
                	DirectionSlow.DOWN;
                	DirectionSlow.UP_DOWN;
                	DirectionSlow.UP;
                	DirectionSlow.DOWN;
                }
                		
                Console.stop();
                
                Console.print("Namespace Access:");
                
                Console.start();
                
                for(i = 0; i < 1000000; i++)
                {
                	Direction.UP_DOWN;
                	Direction.UP;
                	Direction.DOWN;
                	Direction.UP_DOWN;
                	Direction.UP;
                	Direction.DOWN;
                	Direction.UP_DOWN;
                	Direction.UP;
                	Direction.DOWN;
                }
                	
                Console.stop();
                

                On my system it's 540ms vs 387ms. It's not super critical, but if possible I'd recommend using namespaces.

                1 Reply Last reply Reply Quote 1
                • d.healeyD
                  d.healey
                  last edited by

                  Thank you

                  Libre Wave - Freedom respecting instruments and effects
                  My Patreon - HISE tutorials
                  YouTube Channel - Public HISE tutorials

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post

                  29

                  Online

                  1.8k

                  Users

                  12.1k

                  Topics

                  105.2k

                  Posts