Jul 24 | 7:25 PM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Jul 24 | 7:35 PM |
EGHDK | has entered the room |
Mark M. |
hello EGHDK
|
Mark M. |
how can I help you today?
|
EGHDK |
Hey Mark. So a few questions today on my list. How do you speed test things in java/android? Mostly refering to how people have researched doing things (such as a random number generator or something) and testing how long it takes for 1000 numbers to be generated in one way vs another?
|
EGHDK |
Do they just take a timestamp before, and take one after?
|
Mark M. |
for something simple like that, probably
|
Mark M. |
personally, I don't find those sorts of microbenchmarks to be all that useful
|
EGHDK |
How so?
|
Mark M. |
first, in the case of the random number generator, if 1000 takes more than a millisecond, your random number generator algorithm sucks
|
Mark M. |
and once you get into timing millions of the things, you then have to ask yourself: in what circumstances will I ever be *using* millions of these things
|
EGHDK |
1000 random numbers can be generated in one ms?
|
Mark M. |
I'd damn well better hope so
|
Mark M. |
we could do that a decade ago, on much weaker hardware
|
EGHDK |
Hah. Yeah. I guess i'm still amazed at how fast a cpu can go through lines of code.
|
Jul 24 | 7:40 PM |
Mark M. |
I'd rather worry about real-world scenarios (e.g., lots of calls to getView() of a ListAdapter)
|
Mark M. |
and for those, you usually need something like Traceview to figure out where the time is being spent
|
EGHDK |
Yeah. My question is actually because I found some android code golf things, and I'm just working through them.
|
Mark M. |
oh
|
EGHDK |
One of them was generate 10k random values, and then hash them, then populate them in a list view real time. And so I was a little stumped at what the best way to do it is.
|
EGHDK |
I could just make an AsyncTask that generates a random number, then hashes it. And do that 10k times.
|
EGHDK |
But then, I started thinking about multiple cores, and whether or not that something I can optimize for.
|
Mark M. |
I'm not sure what the value is in hashing a random number, even in a code golf scenario
|
EGHDK |
So if I have an android phone thats dual core, I could make 2 async tasks do 5k each?
|
Mark M. |
um, sure, I guess
|
Mark M. |
forking the task and all that may take more time than it would to generate the hashed random numbers
|
Mark M. |
particularly with code golf, you start with the simplest possible solution, and go from there
|
EGHDK |
Yeah, I guess not either. But I didn't really care about the code golf scenario at this point. Mostly like... if I didn't care what order these numbers populated a list... what would be the best way to do this?
|
Jul 24 | 7:45 PM |
Mark M. |
and this is why I don't like code golf and similar "how many angels dance on the head of a pin" sorts of scenarios
|
EGHDK |
Sorry erm.
|
EGHDK |
When I said dont care
|
EGHDK |
I meant tossing away the whole listView thing, but something maybe more practical/time consuming.
|
EGHDK |
So lets say I wanted to generate 10k random values, insert them into a database, and have a listview update from the db.
|
EGHDK |
Would you do something like create your own thread to generate the numbers? Or use an asynctask?
|
Mark M. |
generating the numbers is trivial compared to the disk I/O
|
Mark M. |
hence, don't worry about the numbers
|
Mark M. |
worry about the disk I/O
|
EGHDK |
Very good point.
|
Mark M. |
now, in the case of SQLite, it's effectively single-threaded
|
Mark M. |
and so you may as well just have one thread handle the numbers and the inserts
|
EGHDK |
So it wouldn't make sense to... have two threads generate... and after each thread is done generating it will do an insert... and that insert is single threaded. No real way around that... huh?
|
Jul 24 | 7:50 PM |
Mark M. |
right
|
Mark M. |
moreover, you really really really really really don't want to do 10,000 individual inserts
|
Mark M. |
you want to do 10,000 inserts in one transaction
|
Mark M. |
or at most a few transactions
|
EGHDK |
Aha.
|
EGHDK |
Did not think of that.
|
EGHDK |
How would you combine multiple inserts?
|
Mark M. |
do them in a transaction
|
EGHDK |
Will look into that. Thanks Mark. That makes sense.
|
Mark M. |
or, perhaps 1,000 per transaction
|
Mark M. |
or something like that
|
EGHDK |
Yeah, there probably a sweet spot when working with a fixed number like 10k
|
EGHDK |
Okay. So my next question has to do with inner static classes.
|
Jul 24 | 7:55 PM |
Mark M. |
those are usually referred to static inner classes
|
Mark M. |
"inner class" being a thing, with "static" as a modifier
|
EGHDK |
Got it. Is there such thing as just an inner class thats not static?
|
Mark M. |
sure
|
Mark M. |
it's only usable inside an instance of the outer class
|
Mark M. | |
Mark M. |
IconicAdapter is a regular inner class in that sample's activity
|
EGHDK |
Gotcha. So you wouldn't be able to call it from another class.
|
Mark M. |
right
|
Mark M. |
static inner classes are a convenience mechanism, for something that just as easily could be a standalone Java class
|
Mark M. |
basically, it saves you a file
|
Mark M. |
and, usually, the static inner class is related to the outer class in some way
|
EGHDK |
So I guess I'm just overthinking it, but I'm just blown away at what the difference is between a AlertDialog and AlertDialog.Builder... why two separate classes?
|
Mark M. |
a Builder is a class with a dedicated API optimized for building something
|
Mark M. |
in this case, AlertDialog.Builder has an API optimized for building an AlertDialog instance
|
EGHDK |
Is that a convention?
|
Mark M. |
more or less
|
EGHDK |
building aka .addThis().addThat().create(); type of thing?
|
Mark M. |
right (also called a "fluent API")
|
Mark M. |
Android tends to refer to those as Builders
|
Mark M. |
though other classes, like Intent, offer similar sorts of chainable metods
|
Mark M. |
er, methods
|
EGHDK |
Got it.
|
Jul 24 | 8:00 PM |
EGHDK |
So last thing about AlertDialog.Builder... I'm confused why it has a show() AND a create()
|
EGHDK |
show() effectively creates... but then why would anyone just create() without showing?
|
EGHDK |
and you wouldn't do create() and then call show(), because that doesn't make sense. Wasting a time because it's already been created, so it would be created like twice.
|
Mark M. |
well, ideally, you don't use either of those nowadays, but instead use a DialogFragment to manage the dialog
|
EGHDK |
-_-
|
EGHDK |
Really?
|
Mark M. |
yes
|
Mark M. |
the problem with dialogs is dealing with configuration changes, like screen rotations
|
Mark M. |
the recommended solution for that nowadays is a DialogFragment
|
EGHDK |
Got it.
|
EGHDK |
So I would most set up AlertDialog.Builder, not call create or show and just pass it into a DialogFragment (via a constructor I'm guessing)?
|
Mark M. |
usually the DialogFragment creates the AlertDialog.Builder itself
|
Mark M. | |
Mark M. |
there, the DialogFragment uses create() to return the dialog, which DialogFragment then shows at an appropriate point, using the show() method on AlertDialog (versus show() on AlertDialog.Builder)
|
Jul 24 | 8:05 PM |
EGHDK |
Got it.
|
EGHDK |
Thanks
|
EGHDK |
Learned about java asserts... how would I go about those in android.
|
EGHDK |
Since in java there are flags to turn them on/off
|
Jul 24 | 8:10 PM |
EGHDK |
I was reading in my book that when someone runs your java code and you need debug help you can tell them to run your code with asserts turned on.
|
EGHDK |
Is that a thing in android
|
Mark M. |
I have never seen assertions used in Android development
|
EGHDK |
Hahah. Okay. thanks
|
Jul 24 | 8:15 PM |
EGHDK |
I was watching a video on pluralsight.com. They have android dev video tutorials, and I watched one for about 10 minutes and he creates a new thread, and then goes on to say "threads running for more than a few second may be killed by the os" Is that true?
|
Mark M. |
no
|
Mark M. |
Android may terminate your process
|
Mark M. |
for reasons that we have discussed previously IIRC
|
Mark M. |
but it does not kill off random threads that you fork
|
EGHDK |
Didn't think so. I've been using threads like crazy since I learned how to use them, and theyve been working just fine.
|
Jul 24 | 8:20 PM |
EGHDK |
Okay, so lastly a couple of questions about maven and gradle and ant I guess.
|
EGHDK |
So android w/ eclipse uses the ant build system.
|
EGHDK |
Is it possible to use maven in eclipse with android?
|
Mark M. |
yes
|
Mark M. |
there's an m2eclipse plugin, if I remember the name correctly
|
EGHDK |
So what is maven used for... dependencies (afaik libraries?)
|
Mark M. |
Maven is a full build system, roughly analogous to Gradle (Android's upcoming build system) and Ant
|
Mark M. |
much more powerful than Ant
|
Mark M. |
probably comparable to Gradle, though I haven't used Maven
|
Jul 24 | 8:25 PM |
Mark M. |
Maven also standardized the "artifact repository" structure that is used by Gradle and other tools, and hosts the largest such repository (Maven Central)
|
EGHDK |
But doesn't android studio use gradle which uses maven?
|
Mark M. |
Android Studio uses Gradle
|
Mark M. |
Gradle does not "use" Maven; it supports Maven-style artifacts
|
Mark M. |
it's the difference between using Microsoft Word and supporting DOC/DOCX files
|
Mark M. |
(roughly speaking)
|
EGHDK |
"artifact"? == library?
|
Mark M. |
library with metadata, to define dependencies (library1 depends upon library2) and versioning (this is version X.Y.Z of library1)
|
Mark M. |
in Android's case, "library" is a JAR or an AAR (compiled Android library project)
|
Mark M. |
artifacts can also be a ZIP of JavaDocs, a ZIP of source files, etc., though those are somewhat less common
|
EGHDK |
okay, so d.android.com says "Maven-based build dependencies" YES in Android Studio. SO this doesn't mean MAVEN, it just supports something "like" maven?
|
Mark M. |
let's go back to my earlier analogy
|
Mark M. |
Microsoft Word is a piece of software
|
Mark M. |
a DOCX file is a documen
|
Mark M. |
er, document
|
Mark M. |
Microsoft Word can work with DOCX files
|
Mark M. |
other things can also work with DOCX files
|
Mark M. |
Microsoft defined the DOCX format
|
Mark M. |
Microsoft uses that format for Word, and other software can also support that format
|
EGHDK |
Got it. I just picked up interest because I saw a friend use Android studio, and he said he can't get used to it because it needs gradle to be online or something. He new to android development, but said he doesn't know why it needs to be online.
|
Mark M. |
Gradle doesn't need to be online all the time, but it usually does need Internet access the first time you build the project
|
Mark M. |
as it downloads your dependencies (both build tool dependencies and runtime dependencies) from places like Maven Central
|
Jul 24 | 8:30 PM |
EGHDK |
So I always added projects (like ABS) to my project by downloading it... in Android Studio... you just tell it to go fetch it instead?
|
Mark M. |
once it has a full copy of everything, it can work offline
|
Mark M. |
you download the project still
|
Mark M. |
but the project's dependencies might be obtained from Maven Central or elsewhere
|
EGHDK |
But it does that behind the scenes... right?
|
Mark M. |
well, if by "behind the scenes" you mean "when it does a build", then yes
|
Mark M. |
if it does not have a local copy of a dependency, it tries to download it
|
EGHDK |
Alright. 8:30 thanks. I would love to learn more, especially because I saw your blog that youre doing a gradle thing...
|
EGHDK |
but alas... that is out of my price range.
|
Mark M. |
there are several chapters on Gradle for Android in my book
|
EGHDK |
Yeah, I guess I will read that once android studio is out of beta.
|
Mark M. |
and that's a wrap for today's chat
|
EGHDK |
I feel like I hear that its not ready yet for primetime.
|
Mark M. |
the next chat is Tuesday at 7:30pm
|
Mark M. |
and you're welcome to ask about Android Studio then :-)
|
Mark M. |
the transcript for this chat will be posted to http://commonsware.com/office-hours/ shortly
|
Mark M. |
have a pleasant evening!
|
EGHDK | has left the room |
Mark M. | turned off guest access |