Jump to content

Making setting variables on userinput more efficient.

Go to solution Solved by Ciccioo,

that's a weird situation to work with

 

anyway, it's not really a performance problem, but maybe more of a readibility problem

you could remove the longest case sequence

                        case 1:                        case 3:                        case 4:                        case 6:                        case 8:                        case 9:                        case 11:                        case 13:                        case 17:                        case 18:                        case 24:                                kantone_preis = 0.1997;                                break;

and replace it with a

                        default:                                kantone_preis = 0.1997;

but that would just make the code shorter and worse

 

________________________________________________________________________________

other method: you could go bananas and decide that your key numbers are bits, so you have a 32 bits integer and

                        case 5:                        case 12:                        case 14:                        case 22:

means that the 5th, 12th, 14th and 22nd bits are 1

so you have

00000000010000000101000000100000bin = 4214816dec

if i didn't fuck up writing the binary thing (note: you start counting from the 0th bit)

 

now you have a mathematical bond between your numbers and the action that you need to perform

if( Math.pow(2, kantone_spin_pos) & 4214816 != 0)               kantone_preis = 0.2197;

or, with more elegance

int userChoice = Math.pow(2, kantone_spin_pos);int group1 = 4214816;if( userChoice & group1 != 0) // if the user choice belongs to the first group of numbers               kantone_preis = 0.2197;

i'll leave here the results for the other cases as well, just in case you need them

[2, 7, 15, 16, 19, 20, 23] = 10059908
[3, 4, 6, 8, 9, 11, 13, 17, 18, 24] = 17181528
[5, 12, 14, 22] = 4214816
[10, 21] = 2098176

 

i don't have a java compiler now, i can't be sure that the code is 100% working

Hi guys. I'm relativley new to java.

 

I'm writing an app for android that can calculate power costs depending on user input.
Now I need to set a view variables via Dropdown menus/Spinners.

I've got some code here:

http://pastebin.com/mLV14MRk

 

I know that //stunden and //tage are about as efficient as it gets. But is there any way I could make the first part (//kantone) more efficient?

 

Thanks for your replies.

btw I use arch

Link to comment
Share on other sites

Link to post
Share on other sites

that's a weird situation to work with

 

anyway, it's not really a performance problem, but maybe more of a readibility problem

you could remove the longest case sequence

                        case 1:                        case 3:                        case 4:                        case 6:                        case 8:                        case 9:                        case 11:                        case 13:                        case 17:                        case 18:                        case 24:                                kantone_preis = 0.1997;                                break;

and replace it with a

                        default:                                kantone_preis = 0.1997;

but that would just make the code shorter and worse

 

________________________________________________________________________________

other method: you could go bananas and decide that your key numbers are bits, so you have a 32 bits integer and

                        case 5:                        case 12:                        case 14:                        case 22:

means that the 5th, 12th, 14th and 22nd bits are 1

so you have

00000000010000000101000000100000bin = 4214816dec

if i didn't fuck up writing the binary thing (note: you start counting from the 0th bit)

 

now you have a mathematical bond between your numbers and the action that you need to perform

if( Math.pow(2, kantone_spin_pos) & 4214816 != 0)               kantone_preis = 0.2197;

or, with more elegance

int userChoice = Math.pow(2, kantone_spin_pos);int group1 = 4214816;if( userChoice & group1 != 0) // if the user choice belongs to the first group of numbers               kantone_preis = 0.2197;

i'll leave here the results for the other cases as well, just in case you need them

[2, 7, 15, 16, 19, 20, 23] = 10059908
[3, 4, 6, 8, 9, 11, 13, 17, 18, 24] = 17181528
[5, 12, 14, 22] = 4214816
[10, 21] = 2098176

 

i don't have a java compiler now, i can't be sure that the code is 100% working
Link to comment
Share on other sites

Link to post
Share on other sites

Ciccioo that's one of the best answers I've come across today! Thank you very much. I'll try that out and report back to you.

btw I use arch

Link to comment
Share on other sites

Link to post
Share on other sites

Ciccioo that's one of the best answers I've come across today! Thank you very much. I'll try that out and report back to you.

hopefully it actually works

reading my answer again, i'm not sure if it's actually clear how it works, feel free to ask

Link to comment
Share on other sites

Link to post
Share on other sites

hopefully it actually works

reading my answer again, i'm not sure if it's actually clear how it works, feel free to ask

Works flawlessly! Thank you again. Wasn't sure at first, but after rereading it was pretty clear.

btw I use arch

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×