Office Hours — Today, July 24

Tuesday, July 22

Jul 24
7:25 PM
Mark M.
has entered the room
Mark M.
turned on guest access
7:35 PM
EGHDK
has entered the room
Mark M.
hello EGHDK
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?
Do they just take a timestamp before, and take one after?
Mark M.
for something simple like that, probably
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
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
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.
7:40 PM
Mark M.
I'd rather worry about real-world scenarios (e.g., lots of calls to getView() of a ListAdapter)
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.
I could just make an AsyncTask that generates a random number, then hashes it. And do that 10k times.
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
forking the task and all that may take more time than it would to generate the hashed random numbers
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?
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.
When I said dont care
I meant tossing away the whole listView thing, but something maybe more practical/time consuming.
So lets say I wanted to generate 10k random values, insert them into a database, and have a listview update from the db.
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
hence, don't worry about the numbers
worry about the disk I/O
EGHDK
Very good point.
Mark M.
now, in the case of SQLite, it's effectively single-threaded
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?
7:50 PM
Mark M.
right
moreover, you really really really really really don't want to do 10,000 individual inserts
you want to do 10,000 inserts in one transaction
or at most a few transactions
EGHDK
Aha.
Did not think of that.
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
or something like that
EGHDK
Yeah, there probably a sweet spot when working with a fixed number like 10k
Okay. So my next question has to do with inner static classes.
7:55 PM
Mark M.
those are usually referred to static inner classes
"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
it's only usable inside an instance of the outer class
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
static inner classes are a convenience mechanism, for something that just as easily could be a standalone Java class
basically, it saves you a file
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
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")
Android tends to refer to those as Builders
though other classes, like Intent, offer similar sorts of chainable metods
er, methods
EGHDK
Got it.
8:00 PM
EGHDK
So last thing about AlertDialog.Builder... I'm confused why it has a show() AND a create()
show() effectively creates... but then why would anyone just create() without showing?
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
-_-
Really?
Mark M.
yes
the problem with dialogs is dealing with configuration changes, like screen rotations
the recommended solution for that nowadays is a DialogFragment
EGHDK
Got it.
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
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)
8:05 PM
EGHDK
Got it.
Thanks
Learned about java asserts... how would I go about those in android.
Since in java there are flags to turn them on/off
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.
Is that a thing in android
Mark M.
I have never seen assertions used in Android development
EGHDK
Hahah. Okay. thanks
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
Android may terminate your process
for reasons that we have discussed previously IIRC
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.
8:20 PM
EGHDK
Okay, so lastly a couple of questions about maven and gradle and ant I guess.
So android w/ eclipse uses the ant build system.
Is it possible to use maven in eclipse with android?
Mark M.
yes
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
much more powerful than Ant
probably comparable to Gradle, though I haven't used Maven
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
Gradle does not "use" Maven; it supports Maven-style artifacts
it's the difference between using Microsoft Word and supporting DOC/DOCX files
(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)
in Android's case, "library" is a JAR or an AAR (compiled Android library project)
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
Microsoft Word is a piece of software
a DOCX file is a documen
er, document
Microsoft Word can work with DOCX files
other things can also work with DOCX files
Microsoft defined the DOCX format
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
as it downloads your dependencies (both build tool dependencies and runtime dependencies) from places like Maven Central
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
you download the project still
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
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...
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
and you're welcome to ask about Android Studio then :-)
the transcript for this chat will be posted to http://commonsware.com/office-hours/ shortly
have a pleasant evening!
EGHDK
has left the room
Mark M.
turned off guest access

Tuesday, July 22

 

Office Hours

People in this transcript

  • EGHDK
  • Mark Murphy