David John Welsh
David John Welsh

Reputation: 1569

Which is faster, int to String or String to int?

This may seem like a fairly basic question, for which I apologise in advance.

I'm writing an Android app that uses a set of predefined numbers. At the moment I'm working with int values, but at some point I will probably need to use float and double values, too.

The numbers are used for two things. First, I need to display them to the user, for which I need a String (I'm creating a custom View and drawing the String on a Canvas). Second, I need will be using them in a sort of calculator, for which they obviously need to be int (or float/double).

Since the numbers are the same whether they are used as String or int, I only want to store them once (this will also reduce errors if I need to change any of them; I'll only need to change them in the one place).

My question is: should I store them as String or as int? Is it faster to write an int as a String, or to parse an int from a String? My gut tells me that parsing would take more time/resources, so I should store them as ints. Am I right?

Upvotes: 6

Views: 3111

Answers (8)

Kaj
Kaj

Reputation: 10959

A human who is using the calculator will not notice a performance difference, but as others have said. Using strings as your internal representation is a bad idea since you don't get type safety in that case.

You will most likely get into maintenance problems later on if you decide to use strings.

Upvotes: 2

Dalshim
Dalshim

Reputation: 315

I did a test on a 1 000 000 size array of int and String. I only timed the parsing and results says :

  • Case 1, from int to String : 1 000 000 in an average of 344ms
  • Case 2, from String to int : 1 000 000 in an average of 140ms

Conclusion, you're guts were wrong :) !

And I join the others saying, this is not what is going to make you're application slow. Better concentrate on making it simpler and safer.

Upvotes: 6

paxdiablo
paxdiablo

Reputation: 882616

Actually, your gut may be wrong (and I emphasise may, see my comments below on measuring). To convert a string to an integer requires a series of multiply/add operations. To convert an integer to a string requires division/modulo. It may well be that the former is faster than the latter.

But I'd like to point out that you should measure, not guess! The landscape is littered with the corpses of algorithms that relied on incorrect assumptions.

I would also like to point out that, unless your calculator is expected to do huge numbers of calculations each second (and I'm talking millions if not billions), the difference will be almost certainly be irrelevant.

In the vast majority of user-interactive applications, 99% of all computer time is spent waiting for the user to do something.

My advice is to do whatever makes your life easier as a developer and worry about performance if (and only if) it becomes an issue. And, just to clarify, I would suggest that storing them in native form (not as strings) would be easiest for a calculator.

Upvotes: 7

Shlublu
Shlublu

Reputation: 11027

The best is to do a bench test. Write two loops

  • one that converts 100000 units from numeric to String
  • one that converts 100000 units from String to numeric

And measure the time elapsed by getting System.currentTimeMillis() before and after each loop.

But personally, if I would need to do calculation on these numbers, I would store them in their native format (int or float) and I would only convert them to String for display. This is more a question of design and maintainability than a question of execution speed. Focusing on execution speed is sometime counterproductive: to gain a few µSec nobody will notice is not worth sacrifying the design and the robustness (of course, some compromise may have to be done when this is a question of saving a lot of CPU time). This reading may interest you.

Upvotes: 1

Vivien Barousse
Vivien Barousse

Reputation: 20895

You are writing an application for mobile devices, where the memory comsumption is a huge deal.

Storing an int is cheap, storing a String is expensive. Go for int.

Edit: more explanation. Storing an int bteween -2^31 and 2^31-1 costs 32 bits. No matter what the number is. Storing it in a String is 16 bits per digit in its base 10 representation.

Upvotes: 1

nes1983
nes1983

Reputation: 15756

Parsing is a slow thing, printing a number is not. The internal representation as number allows you to compute, which is probably what you intend to d with your numbers. Storing numbers as, well, numbers (ints, floats, decimals) also takes up less space than their string representations, so … you'll probably want to go with storing them as ints, floats, or whatever they are.

Upvotes: 0

Thomas
Thomas

Reputation: 88757

I'd say that's not really relevant. What should matter more is type safety: since you have numbers int (or float and double) would force you to use numbers and not store "arbitrary" data (which String would allow to some extent).

Upvotes: 3

Oliver
Oliver

Reputation: 11617

It's better design practice to have the view displayed to the user being derived from the underlying data, rather than the other way around - at some point you might decide to render the calculator using your own drawing functions or fixed images, and having your data as strings would be a pain here.

That being said, neither of these operations are particularly time consuming using modern hardware.

Upvotes: 0

Related Questions