10 Replies Latest reply: Mar 21, 2010 10:55 AM by 843789 RSS

    Delegates

    800606
      In C# you can do something like
      EventHandler event=delegate{
          // some code here
      };
      ...
      if(a==1){
          event=delegate{
              // another code here
          };
      }
      else{
          event=delegate{
             // yet another code here
          };
      }
      ...
      // then you envoke the event based on the above definition
      event.Envoke(this,null);
      Is this possible with java? I dont wanna use if statements because a can have 100 different values - 100 different events and the event would be run every 20-30 ms (so if/switch-case stuff would slow it down).
        • 1. Re: Delegates
          843789
          alexxzius wrote:
          [snip]
          // then you envoke the event based on the above definition
          event.Envoke(this,null);
          Are you sure you mean "envoke" and not invoke?

          What exactly is the advantage of doing this instead of simply running the code in the if-else directly?

          You may get something similar by assigning to a Runnable variable and just calling myVar.run() at the end.
          Is this possible with java? I dont wanna use if statements because a can have 100 different values - 100 different events and the event
          [...] would be run every 20-30 ms (so if/switch-case stuff would slow it down).
          No, "if/switch-case stuff" doesn't slow anything down ...

          That might be your intuition, but it's wrong. And once ever 20-30 ms is very, very long in CPU time. Once ever 20ms is just 50 times a second on a processor where the speed is most likely measured in gigahertz (that's billions of instructions per seconds!)
          • 2. Re: Delegates
            843789
            Why don't you just put the code that would run in the block itself? Why create a delegate at all?
            If you don't want to use an if or switch statement, how are you going to set the event variable?
            I seriously doubt a switch statement will be much slower than a method call. It could even be faster.

            Assuming the if/switch is only once and you want to call the Envoke or Invoke method many time you could just use an enum to do this, each value of your enum can have a different implementation of a method.

            As has been noted, 20 ms is about 40,000,000 clock cycles or more. A long time for a computer.
            • 3. Re: Delegates
              800606
              Checking the same variable 100 times over 50 times is second is very redundant. It's like doing
              int a=5,b;
              if(a==1)b=2;
              else if(a==2)b=5;
              ...
              else if(a=100)b=1;
              where you could just store b values in an array and get them by array[a].
              What exactly is the advantage of doing this instead of simply running the code in the if-else directly?
              Well, I will be setting the delegate about once a minute. And then it would run thousands of times without any of the if stuff.
              • 4. Re: Delegates
                843789
                alexxzius wrote:
                Checking the same variable 100 times over 50 times is second is very redundant. It's like doing
                int a=5,b;
                if(a==1)b=2;
                else if(a==2)b=5;
                ...
                else if(a=100)b=1;
                where you could just store b values in an array and get them by array[a].
                I see, but your example above didn't show that all your conditions are so similar and the action exactly the same (which is the condition to replacing the if-else cascade with a lookup).

                You can also use a switch, which can easily be compiled into a lookup-jump, and will be similarly efficient.
                What exactly is the advantage of doing this instead of simply running the code in the if-else directly?
                Well, I will be setting the delegate about once a minute. And then it would run thousands of times without any of the if stuff.
                Still, did you measure that the "if-stuff" is the bottleneck?

                Anyway, did my Runnable-idea help?
                • 5. Re: Delegates
                  800606
                  Still, did you measure that the "if-stuff" is the bottleneck?
                  I'm not saying that it will crash the application. I'm just saying it's so very inefficient.
                  Anyway, did my Runnable-idea help?
                  How would I do that? Runnable is an interface, right?
                  • 6. Re: Delegates
                    843789
                    alexxzius wrote:
                    Still, did you measure that the "if-stuff" is the bottleneck?
                    I'm not saying that it will crash the application. I'm just saying it's so very inefficient.
                    "so very inefficient" makes me think that you might apply premature optimization. Be wary of that!
                    Anyway, did my Runnable-idea help?
                    How would I do that? Runnable is an interface, right?
                    Right, and you implement it with different code.
                    • 7. Re: Delegates
                      800606
                      Right, and you implement it with different code.
                      I tried this, but it didnt work
                      Runnable runnable={
                          System.out.println("Hello");
                      };
                      runnable.start();
                      Any links for that?
                      • 8. This Thread is now moved
                        DarrylBurke
                        Note: This thread was originally posted in the [Java Programming|http://forums.sun.com/forum.jspa?forumID=31] forum, but moved to this forum for closer topic alignment.
                        • 9. Re: Delegates
                          DarrylBurke
                          [http://java.sun.com/docs/books/tutorial/java/IandI/usinginterface.html]

                          db
                          • 10. Re: Delegates
                            843789
                            alexxzius wrote:
                            Still, did you measure that the "if-stuff" is the bottleneck?
                            I'm not saying that it will crash the application. I'm just saying it's so very inefficient.
                            The difference could be as much as 0.4 ns. Is this really what you call "very inefficient". If so you can buy a faster processor than I am using and cut the gap to be less than 0.2 ns. How much of your time is worth spending to save this much time?

                            The following prints that it takes about 8.6 ns to perform a switch statement and set the variable.
                            public class P {
                                private static void test(boolean print) {
                                    int a = 50;
                                    long start = System.nanoTime();
                                    for (int i = 0; i <= 100; i++) {
                                        int b = 0;
                                        switch (a) {
                                        case 1:
                                            b = 1;
                                            break;
                            
                                        case 2:
                                            b = 2;
                                            break;
                            
                            // code deleted
                                        case 99:
                                            b = 99;
                                            break;
                            
                                        case 100:
                                            b = 100;
                                            break;
                                    }
                                    }
                                    long time = System.nanoTime() - start;
                                    if (print)
                                        System.out.printf("Took an average of %4.1f ns to perform a switch statement%n", time / 102.0);
                                }
                            
                                public static void main(String... args) {
                                    for (int i = 1; i <= 1000; i++) {
                                        test(false);
                                    }
                                    test(true);
                                }
                            }
                            The following prints that it takes about 8.2 ns to perform a method call and set the variable.
                            public class P {
                                interface Delegate {
                                    public int invoke(Object o, Object o2);
                                }
                            
                                private void test(boolean print) {
                                    Delegate delegate = new Delegate() {
                                        public int invoke(Object o, Object o2) {
                                            return 50;
                                        }
                                    };
                                    long start = System.nanoTime();
                                    int b;
                                    for (int a = 0; a <= 100; a++) {
                                        b = delegate.invoke(this, null);
                                    }
                                    long time = System.nanoTime() - start;
                                    if (print)
                                        System.out.printf("Took an average of %4.1f ns to perform a switch statement%n", time / 102.0);
                                }
                            
                                public static void main(String... args) {
                                    for (int i = 1; i <= 1000; i++) {
                                        new P().test(false);
                                    }
                                    new P().test(true);
                                }
                            }