At the end of the day? Well, one can say you don't have to worry!
But, under the hood? Ok, I'll bite!!
yes, the output tends to be JavaCode, and THEN some REAL magic starts to take place!
The folks at Google and Android spent some REAL serious money. They built a optimizer. (I think it came out around Android 4-6???
The result? Well, the optimizers are REALLY cool and really amazing. They work similar to how the .net compiler works. So in .net you can use C#, vb.net, or I think out of favor was F#. In all 3 cases, they compile down to a p-code system (called IL - or intermediate language). That is the code sent to the platform in question. Then there is the new magic thingy called a JIT. (a just in time compiler).
So for Android? The native language is java, and you can think of that output result VERY similar to the "IL" system in .net
Then toss is some REAL BIG bucks - and a lot of R&D? And a REALLY cool "JIT"?
Well, it then crunches down that code to native Android assembly - on the fly with that JIT. The advantage of this architecture is that you can thus have different CPU's and processors - you only have to re-write that JIT for the different processor.
And how good does it work? Hang on to your underpants!!!! - it works VERY good.
In fact in some technical papers from Google and Android? They state that in some cases you get as good or EVEN better in some cases then HAND coded c++! And the JIT can MAKE decisions!!!! (this is NOT possible with a native pre-compiled code system).
So while running your code? It can say, hay, that is a REALLY bad way to use some memory, and I think we better off to re-use some the CPU registers since that LAST loop and code did NOT run all that great. So it can tweak and change it mind as to how next loop and code executing will occur. If that loop becomes processing intensive? Well, then the JIT can have another go at the code - and re-compile again with HIGHER optimizations. (but why bother for some simple lines of code - just execute them and we are done!
Now for real high speed and some real heaving games for Android? Sure, the developers will write c++ code and use a native compiler.
However, with all these incredible optimizers and those boatloads of money spent on the optimizing system and that JIT?
all that $$$$ spent on this JIT?
You get native assembler CPU speed, and in some cases as noted, you get as good or even BETTER execution of code developed by a experienced c++ coder and pre-compiled code!!! And you GET this using B4A!!!!
On my dumpster fire el-cheap-o "test" mule Android phone? (a budget Moto g)?
(4 cores - 1.4 ghz).
Writing in nice clean simple Basic code - something I done since my Apple II?
Well, on Quroa there was this question: How many 7 digit prime numbers are there?
(basic 1 million to 10 million)
My answer:
So, the number is quite large. There are 8,999,999 (about 9 million) 7 digit long numbers.
So, out of about 9 million existing 7 digit long numbers, 586,081 are primes.
So I needed a big fast computer platform to solve this. Used to have to pay for time on Control Data "Cyber" computer. That computer was in Calgary - funded by the government and some oil companies. It was rated at about 200 mflops.
But, in place of that big mainframe, to answer that question on Quroa about how many prime numbers?
Hum, what computer do I have sitting around? OHHH I know, I use my cheap low cost smartphone and B4A!
After all it only what - we only need to do close to 1 billion calculations - right? How hard is that?
Well, I wrote the code in B4A to find this answer!!! -
Sub PrimeCounter
Dim i As Int
Dim k As Int
Dim Upper As Int
Dim bolPrime As Boolean
Dim PrimeCount As Int = 0
Dim MyTicks As Long
MyTicks = DateTime.Now
Dim lngStart As Int
Dim lngEnd As Int
lngStart = 1000001
lngEnd = 9999999
For i = lngStart To lngEnd Step 2
Upper = Sqrt(i)
k = 3
bolPrime = True
Do While k <= Upper
If i Mod k = 0 Then
bolPrime = False
Exit
End If
k = k + 2
Loop
If bolPrime Then
PrimeCount = PrimeCount + 1
End If
Next
Dim TimeTicks As Long
TimeTicks = DateTime.Now
TimeTicks = TimeTicks - MyTicks
Log("===============")
Log("time = " & (TimeTicks / 10000))
Log("Count of primes found = " & PrimeCount)
EditText1.Text = "time = " & ( (DateTime.Now - MyTicks) / 1000) & _
CRLF & "Count of primes found = " & PrimeCount
the answer? 586,081 primes exist in that range.
The time for that el-cheapo phone?
Well, first run I get this:
13.69 seconds.
If I run it again - then even more of that amazing R & D from google kicks in:
I get this:
8.812 seconds.
(now think about how this 2nd run was OH SO much faster? Those optimizers figured out HOW to do things even better!
So to answer your question?
yes, the software eventually gets crunched down to native arm CPU code. And it is STUPID fast - really!!!
Now of course just looping raw code? That's real fast. You will find that crunching views + display does chew up memory and resources in a "different way", and some of the UI update and rendering can slow things down. But raw code speed?
You get the same speed in B4A as just about any other code system - including that of hand writing some c++ routines that is compiled down to native assembly code all nice and "RAW" for the CPU to chew on!
Those with better phones and newer gen processors can give the above a try (run in release mode), but the speed of your code in terms of base execution speed of code? it is fast - very fast. Its pushing things out to the UI that will bite you, and that still takes care and considering on your part - and that considering exists for just about any all Android SDK's.
The word of the day here? Absolute beyond remarkable how fast this code runs.
to be fair, this code could be split in half, or even 4 loop ranges, and then all 4 cores could run that code. So, to be fair? With better threading support, one could chop this time down in half, or even 1/4th the time to run.
But right now? You can keep your pants on - but B4A is able to blow them right off with some of the speeds I seen it run things.
Regards,
Albert D. Kallal
Edmonton, Alberta Canada