Teo Filimon

Greenhorn
+ Follow
since Aug 18, 2013
Teo likes ...
Android Java
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
2
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Teo Filimon

Hi everyone, just turned this simple domain into a tool that allows people to Test Regex Online.
7 years ago
@James I have my own performance tests to back it up. I've seen that depending on the arraylist size the boost varies between 2x and 3x. Also, please check: http://developer.android.com/training/articles/perf-tips.html#Loops
8 years ago
2 to 3 times faster is not really negligible (but i only mean ArrayList). But if you care more about readability i would also suggest the foreach syntax.
8 years ago
At a second read, i think i understand your question. Normally it doesn't really matter which one you loop (array or arraylist - however making arraylists will prevent you from using primitives which might be costly with boxing/unboxing). There might be some nuances with sorted data structures (slower to add objects, faster to check if it contains certain elements) but it really depends on the case.
8 years ago
I wrote a mini-article a while back about looping ArrayLists. Essentially it's a lot faster when you loop using a good old-fashion counter, like:

But i would only write it like that if i really care about speed. Otherwise, i'm not sure i understand your question or the 2 code samples, i'm not sure they're doing the same thing
8 years ago
Depends on your particular use case. The static block is executed when the class is statically initialized (probably on the startup of your app), whereas the static method is just called when you call it in the code: http://www.jusfortechies.com/java/core-java/static-blocks.php
8 years ago
Normally i do agree with you but i don't think this "performance freak" (which i agree that i am) scenario is that exotic, everyone must judge for themselves. I agree with you that you can reuse objects (if indeed it's possible in the context of the topic starter) but that's only shaving the constructor cost, not the field/method accesses in the extra object. On Android it can make your day if you shave off 10 milliseconds of a response time or if you can save a bit of battery in a service that's running forever because not everyone has a quad-core phone yet. The user will be happy if the app is fast but if it's instantaneous impressed is the better word. It's up to each and every one of us whether we value this overhead or not (the important thing is that we measure it). Sorry, for me clarity and efficiency are on 2 opposite sides of the spectrum and they rarely meet, and the problem is i'm a fan of both of them myself
8 years ago
I just wrote some simple code (stupid by any standard but does the job) to better express what i mean:



And some benchmark code:



I have run this on an Android emulator. For one million iterations (and the proportion is constant for any no. of iterations) i get:
- 12251 millis for instantiating 1 million AB objects
- 22105 millis for instantiating 1 million A objects and 1 million B objects
- and this is the favorable case when:
- i'm not even considering the instantiation of an encapsulating class containing A and B
- i am considering an AB to A and AB to B relation of 1-to-1, because it can also be 1-to-many
8 years ago
Sorry that i sounded so categorical But i was looking to answer the question that started this topic and the truth is:
- if you convert class A to classes B,C,D this will not be faster (if you don't optimize in other ways) because indeed it has a cost
- and the second part of my answer is a way to determine this cost:
- if you insantiate A once and this means B,C,D each get instantiated once it might be worth the cost
- if you instantiate A once but this generates 100 instantiations of B, 100 of C and 100 of D it's already beginning to sound iffy to me and this is where some measurements would help

IMHO classes generally look good in the beginning (OOP wise, clarity wise) and then they evolve towards greater efficiency as you add more and more to them. This is why they lose their nice look. If it all goes into the heap anyway it's perfectly feasible large classes can exist if efficiency is a top priority (as long as someone understands them).
8 years ago
Instantiating classes is an expensive operation. Dividing a big class into sub-classes can be a nice exercise to understand what it does better but it will be slower for sure. And also take into consideration how many times you will need to instantiate B, C and D for every instantiation of A. What i would do in your place (and i have done it ) is test out how long it takes with the big class and then how long it takes with the smaller classes. Compare the durations and if the gain in clarity for the future is worth the performance loss in the present (CPUs tend to get faster as time goes by) go for it. If not, don't
8 years ago
Can't the jLabels be retrieved as a children array/list from their parent UI element?
8 years ago
To be honest i would devise a formula which in the end would result in milliseconds (long in Java) and i would store this in the database.

years*MILLIS_PER_YEAR+months*MILLIS_PER_MONTH+day*MILLIS_PER_DAY

(be careful with the months, days and years, whether you want them 0-based or not)

You can then obtain anything from this formula by using division and modulo.
8 years ago
What do you want to do with this number that you obtain?
8 years ago
By better do you mean faster or simpler?

I'm not sure how much simpler you can go than something like the lines you mentioned without sacrificing code readability. If you care about performance, perhaps looking for space characters with indexOf(char) and indexOf(char,start) and then using substring(start,end) would be slightly faster, e.g.

int firstSpace = x.indexOf(' ');
int secondSpace = x.indexOf(' ', firstSpace+1);
x = substring(0,firstSpace).concat(x.substring(secondSpace, x.indexOf(' ', secondSpace+1)));
8 years ago
Current Millis - convert milliseconds to date and time

I was basically debugging system alarms in Android and to my surprise there was no easily accessible site on Google to show my the current time in millis. Plus, currentmillis.com was not taken So i took it and made the site above. It shows you the milliseconds since the epoch and you can convert from milliseconds to human readable times and dates and back again. Also, you can see live numbers and compare with UTC. If you have suggestions on what else might be useful please tell me.
8 years ago