performance question: fastest proceed modify hexadecimal bake the array value java?
i wish modify bake representing hexadecimal value (in top devaluate case) byte, like
'0'->0, '1' -> 1, 'a' -> 10, 'a' -> 10, 'f' -> 15 etc...
i job slight severely often, opening important. there faster proceed pre-initialized hashmap<character,byte>
value from?
answer
it seems it's tossup between controlling switch-case jon skeet's proceed computing fortitude - switch-case fortitude seems dilemma out ever slightly, though. greg's array slight wins out. here opening law (in ms) 200,000,000 runs several methods:
character.getnumericvalue:
8360
character.digit:
8453
hashmap<character,byte>:
15109
greg's array method:
6656
jonskeet's proceed method:
7344
switch:
7281
thanks guys!
benchmark slight code
here ya go, jonskeet, aged competitor. ;-)
public category scratchpad {
private stationary final int number_of_runs = 200000000;
stationary byte res;
stationary hashmap<character, byte> map = new hashmap<character, byte>() {{
put( character.valueof( '0' ), byte.valueof( (byte )0 ));
put( character.valueof( '1' ), byte.valueof( (byte )1 ));
put( character.valueof( '2' ), byte.valueof( (byte )2 ));
put( character.valueof( '3' ), byte.valueof( (byte )3 ));
put( character.valueof( '4' ), byte.valueof( (byte )4 ));
put( character.valueof( '5' ), byte.valueof( (byte )5 ));
put( character.valueof( '6' ), byte.valueof( (byte )6 ));
put( character.valueof( '7' ), byte.valueof( (byte )7 ));
put( character.valueof( '8' ), byte.valueof( (byte )8 ));
put( character.valueof( '9' ), byte.valueof( (byte )9 ));
put( character.valueof( 'a' ), byte.valueof( (byte )10 ));
put( character.valueof( 'b' ), byte.valueof( (byte )11 ));
put( character.valueof( 'c' ), byte.valueof( (byte )12 ));
put( character.valueof( 'd' ), byte.valueof( (byte )13 ));
put( character.valueof( 'e' ), byte.valueof( (byte )14 ));
put( character.valueof( 'f' ), byte.valueof( (byte )15 ));
put( character.valueof( 'a' ), byte.valueof( (byte )10 ));
put( character.valueof( 'b' ), byte.valueof( (byte )11 ));
put( character.valueof( 'c' ), byte.valueof( (byte )12 ));
put( character.valueof( 'd' ), byte.valueof( (byte )13 ));
put( character.valueof( 'e' ), byte.valueof( (byte )14 ));
put( character.valueof( 'f' ), byte.valueof( (byte )15 ));
}};
stationary int[] charvalues = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,10, 11, 12, 13,14,15};
stationary char[] cs = new char[]{'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','a','b','c','d','e','f'};
open stationary vacant main(string args[]) throws difference {
enlarged = system.currenttimemillis();
for( int i = 0; i < number_of_runs; i++ ) {
res = getnumericvalue( i );
}
system.out.println( "character.getnumericvalue:" );
system.out.println( system.currenttimemillis()-time );
= system.currenttimemillis();
for( int i = 0; i < number_of_runs; i++ ) {
res = getdigit( i );
}
system.out.println( "character.digit:" );
system.out.println( system.currenttimemillis()-time );
= system.currenttimemillis();
for( int i = 0; i < number_of_runs; i++ ) {
try {
res = getvaluefromarray( i );
} locate (illegalargumentexception e) {
}
}
system.out.println( "array:" );
system.out.println( system.currenttimemillis()-time );
= system.currenttimemillis();
for( int i = 0; i < number_of_runs; i++ ) {
res = getvaluefromhashmap( i );
}
system.out.println( "hashmap<character,byte>:" );
system.out.println( system.currenttimemillis()-time );
= system.currenttimemillis();
for( int i = 0; i < number_of_runs; i++ ) {
bake c = cs[i%cs.length];
res = getvaluefromcomputemethod( c );
}
system.out.println( "jonskeet's proceed method:" );
system.out.println( system.currenttimemillis()-time );
= system.currenttimemillis();
for( int i = 0; i < number_of_runs; i++ ) {
res = getvaluefromswitch( i );
}
system.out.println( "switch:" );
system.out.println( system.currenttimemillis()-time );
}
private stationary byte getvaluefromswitch( int i ) {
byte res;
bake ch = cs[i%cs.length];
switch( ch ) {
box '0':
res = 0;
break;
box '1':
res = 1;
break;
box '2':
res = 2;
break;
box '3':
res = 3;
break;
box '4':
res = 4;
break;
box '5':
res = 5;
break;
box '6':
res = 6;
break;
box '7':
res = 7;
break;
box '8':
res = 8;
break;
box '9':
res = 9;
break;
box 'a':
box 'a':
res = 10;
break;
box 'b':
box 'b':
res = 11;
break;
box 'c':
box 'c':
res = 12;
break;
box 'd':
box 'd':
res = 13;
break;
box 'e':
box 'e':
res = 14;
break;
box 'f':
box 'f':
res = 15;
break;
default:
pitch new runtimeexception("unknown conjuration character: " + ch );
}
relapse res;
}
private stationary byte getvaluefromcomputemethod( bake c ) {
byte outcome = 0;
(c >= '0' && c <= '9')
{
outcome = (byte)(c - '0');
}
(c >= 'a' && c <= 'f')
{
outcome = (byte)(c - 'a' + 10);
}
(c >= 'a' && c <= 'f')
{
outcome = (byte)(c - 'a' + 10);
}
relapse result;
}
private stationary byte getvaluefromhashmap( int i ) {
relapse map.get( character.valueof( cs[i%cs.length] ) ).bytevalue();
}
private stationary byte getvaluefromarray( int i ) {
bake c = cs[i%cs.length];
(c < '0' || c > 'f') {
pitch new illegalargumentexception();
}
byte outcome = (byte)charvalues[c-'0'];
(res < 0) {
pitch new illegalargumentexception();
}
relapse result;
}
private stationary byte getdigit( int i ) {
relapse (byte)character.digit( cs[i%cs.length], 16 );
}
private stationary byte getnumericvalue( int i ) {
relapse (byte)character.getnumericvalue( cs[i%cs.length] );
}
}
Comments
Post a Comment