1 Reply Latest reply: Jul 1, 2014 4:12 AM by Sebastien_Lorquet RSS

    Performances issues with java card


      Hello, i'm new to java card technology, and i'm also unaware what is normal execution time on java card. I know it will be slow on java card, and because of it should only deal with simple stuff.


      Although i acknowledge that, i came to really odd figure of execution time of a simple piece of code, the code below takes exactly 1300 ms to run, is it normal?


      I'm executing this in a smartc@fe card 3.2 that has 13 Mhz of processor, and less than 2KB RAM and 72KB of EEPROM.


      If possible could you also test this with the card available to you? if so, please respond and with the specification of the card.




      private static short benchmark()
             short v, n, x, y;
             short x_size, y_size;
             v = 0;
             n = 128;
             x_size = 100;
             y_size = 100;
             for (x = 0; x < x_size; x++)
                for (y = 0; y < y_size; y++)
                  v = (short) ~((v + n) & 0xFF);
              return v;




        • 1. Re: Performances issues with java card



          Well, your card is equivalent to an arduino with boosted NV storage.

          (except the atmega328 is running at 16 MHz)


          Can you imagine running a full java VM (including inheritance and all) and a GlobalPlatform runtime on an arduino?


          I think your runtime performance is normal


          Here is the java bytecode (this is not javacard bytecode) for your method; the javacard bytecode is a little different, but the algorithms are similar:

          this bytecode was produced by javac and disassembled by javap from one of the latest jdk


          locals: (recovered by me)

          0 : v

          1 : n

          2 : x

          3 : y

          4 : x_size

          5 : y_size


          (comments and labels are mine)


            private static short benchmark();



                 0: iconst_0

                 1: istore_0



                 2: sipush        128

                 5: istore_1



                 6: bipush        100

                 8: istore        4



                10: bipush        100

                12: istore        5




                14: iconst_0

                15: istore_2




          if(x>=x_size) goto end_loop_x

                16: iload_2

                17: iload         4

                19: if_icmpge     57 ; end_loop_x




                22: iconst_0

                23: istore_3



          if(y>y_size) goto end_loop_y

                24: iload_3

                25: iload         5

                27: if_icmpge     49; end_loop_y


          compute v+n

                30: iload_0

                31: iload_1

                32: iadd


          and with FF

                33: sipush        255

                36: iand


          complement (xor with -1)

                37: iconst_m1

                38: ixor


          cast to short

               39: i2s


          store in v

                40: istore_0


          y++, with a cast to short

               41: iload_3

                42: iconst_1

                43: iadd

                44: i2s

                45: istore_3



                46: goto          24; start_loop_y




          x++, with a cast to short

               49: iload_2

                50: iconst_1

                51: iadd

                52: i2s

                53: istore_2



                54: goto          16; start_loop_x



          return v

                57: iload_0

                58: ireturn


          As always, this is pretty inefficient because of the stack machine structure, and explains why your code is slow. welcome to java 

          In some code we had some large quantities of the syntax:


          array[index+] = expression;


          and this produced pretty awful code. It was better to hard-code the indices instead, since they were known.


          Java relies on the virtual machine to optimize the bytecode and compile it to native code. This is okay in desktop java, but no javacard I know will optimize (let alone JIT compile!!) any bytecode!