JNI for loop has run incurrectly in C - android

for(int i=0;i<100;i++)
This loop was run repeatly by incrementing value of i by 1.
I have develop in Android JNI.
An OpenGL Renderer's onDrawFrame method calls repeatly.
In onDrawFrame, render function in C++ was call.
in a c++ render function. for loop runs 100 times over.
for(int i=0;i<100;i++)
{
__android_log_print(ANDROID_LOG_DEBUG , "", "%d", i);
}
This loop runs incorrectly.
i value has not incremented by 1.
in other case, loop is not run 100 times. some times 90, 98, or 96.....
I don't know reason.

You cannot trust that __android_log_print() output will be displayed, especially if you are using the logcat window in Eclispe. You can write instead to local file, and then you will for sure see 100 lines in that file after you start the loop.

Related

Measuring Test case Execution Time

I'm trying to measure and increase the execution time of a test case. What I'm doing is the following:
Let's assume I'm testing the method abc() using testAbc().
I'm using android studio and junit for my software development.
At the very beginning I'm recording the timestamp in nano seconds in
start variable, and then when the method finishes, it returns the
difference between the current nano seconds and the start.
testAbc() is divided into 3 parts: initialization, testing abc() and
assertion (checking the test results).
I keep track the test time inside testAbc() same way as I do in
abc() method.
after executing the test I found that abc() method takes about
45-50% of the test time.
I modified the testAbc() as follows:
void testAbc(){
startTime = System.nanoTime();
//no modification to the initialization part
//testing abc part is modified by placing it in a for loop to increase its
//execution time.
for(int i=0 ; i < 100; i++)
{
//test abc code goes here ...
abcTime += abc();
}
//assertion part wasn't modified
testEndTime = System.nanoTime - startTime;
}
By repeating the test part I thought the ratio between abcTime and testEndTime will increase (by dramatically increasing abcTime), however, it didn't at all I mean it's 45-50%.
My questions:
Why the ratio didn't increase? I mean in principle the execution time for the initialization and assertion parts should not be affected by the for loop and therefore the time for abc should get closer to the test time after 100 repetitions.
How can I increase the ratio between abc() and testAbc()?
thank you for your time.

How can I make an object move across my screen gradually using a loop in Lua and Corona Labs SDK

I'm making a game that has obstacles which the player must jump over. I'm using the Corona Labs Simulator to run it but every time I do either the obstacle gets half way across the screen instantly and stops or the whole thing just crashes. Here is my code:
function obstacles()
loop = 2000
while loop > 0 do
obstacle:translate( -1, 0 )
if obstacle.x > 0 then
loop = loop - 1
else
loop = 2000
obstacle:translate( display.contentWidth, 0 )
end
end
end
Any help much appreciated.
Its important to understand that Corona SDK is an event driven system. There really isn't a game loop. Code executes so fast you can't use structures like loops to move things.
As mentioned above, the transition library (transition.to for instance) can move an object over time.
The closest thing to a game loop is to create a function and attach it to the Runtime object using the "enterFrame" event. Every frame (either 30 times per second or 60 times per second), this function will be called. It's not going to be precisely 30 or 60 times per second because it make take too much time doing other work to give you the full frame rate. If you want to use an enterFrame listener:
local function doSomethingEachFrame( event )
-- put code here you want to execute over time.
-- just remember it fires a lot...
end
Runtime:addEventListener( "enterFrame", doSomethingEachFrame )
If you need to stop it, then you would do:
Runtime:removeEventListener( "enterFrame", doSomethingEachFrame )
Corona SDK provides tools to do exactly that. Rather than using a loop, use transition library:
transition.to(target, params)
where target is your obstacle object, and in params, you can specify x position, time etc.
There can be a few sources of weirdness regarding where things appear on-screen. I'd put obstacle at (0,0) with no movement at all and make sure I understand both the coordinate system of the parent, and the anchorX and anchorY of obstacle before trying to animate it.
I assume there's other code outside the block you've included, but I think you can make the movement logic simpler, and you'll thank yourself later (this isn't perfect, but you get the idea):
-- set initial position
obstacle.x = display.contentWidth
-- define per-frame movement logic
local moveObstacle = function(event)
obstacle.x = obstacle.x - 1
if obstacle.x < 0 then
obstacle.x = display.contentWidth
end
end
-- run that logic once every frame
Runtime:addEventListener("enterFrame", moveObstacle)

Python Kivy: Add Background loop

I want to paste a background loop into my Python-Kivy script. The problem is, that I've got only a App().run() under my script. So, if I put a loop, somewhere in the the App-Class, the whole App stopps updating and checking for events. Is there a function name like build(self), that's recognized by Kivy, and represents a main/background-loop?
If you don't know, what I'm talking about, feel free to ask.
In case you need to schedule a repeated activity in a loop, you can use Clock.schedule_interval() to call a function on a regular schedule:
def my_repeated_function(data):
print ("My function called.")
Clock.schedule_interval(my_repeated_function, 1.0 / 30) # no brackets on function reference
# call it 30 times per second
There is a lot more information on how to schedule events on a regular, conditional or one-time basis with Kivy's event loop here.

How can I iterate through beacons with android-ibeacon-service?

I'm using the beacon ranging methods to discover and use iBeacons. However, I have run into problem while trying to iterate through all the beacons that were found.
The beacon service returns a collection, which I can check the size of and see there are 3 beacons. However, not matter what I try I can reliably iterate through the beacons. I seem to go in a different random order of beacons every time, and some are frequently skipped over.
I have tried while(iBeacons.iterator().hasNext()) and that just froze my app. I have tried comparing the lastBeacon which I parsed to the current beacon selected and throwing it out, but this didnt work to get through all beacons.
I think that since the collection is being update asynchronously and my parsing takes far longer than a second new beacons have been added before I ever get a chance to process the old ones, and I seems to just randomly process one from the collection each time.
How can I get and parse each beacon each time?
The code is close -- it simply needs to construct an Iterator on a separate line like this:
Iterator<IBeacon> iterator = iBeacons.iterator();
while (iterator.hasNext())
The reason your the freezes has nothing to do with asynchronous updates by the library. (The list of iBeacons passed to the ranging callback is a copy of those seen in the last scan cycle and never gets changed.) The problem has to do with the way the iterator() method works in Java.
In the code above, the while(iBeacons.iterator().hasNext()) creates a new copy of Iterator every time inside the loop, and that iterator is always pointing at the first element in the iBeacons collection. So if there is ever at least one item in that collection when the loop starts, it will never finish, and the code will freeze.
You can demonstrate this with the following example:
public class IteratorTest{
public static void main(String []args){
java.util.Collection iBeacons = java.util.Arrays.asList("1", "2", "3");
System.out.println("This loop will not freeze");
java.util.Iterator iterator = iBeacons.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
System.out.println("This loop will freeze");
while(iBeacons.iterator().hasNext()) {
System.out.println(iBeacons.iterator().next());
}
// We will never get to here
}
}
Which produces the following (never ending) output:
This loop will not freeze
1
2
3
This loop will freeze
1
1
1
1
1
1
1
1
1
1
1
1
1

Android Multi Threading with native code

I am working on an android project and found that an operation becomes bottleneck in performance. This operation works on a large array A and stores the result into another array B.
I found that this operation can be parallelized. The array A can be divided into N smaller segments. The operation can work on each segment independently and store the result into a corresponding segment in B.
The operation is written in native code with GetPrimitiveArrayCritical/ReleasePrimitiveArrayCritical pairs to access array A and B.
My question is that if using multi threading, GetPrimitiveArrayCritical(pEnv, A, 0) will be called multiple times from different threads. Does GetPrimitiveArrayCritical block? i.e. if one thread makes this call, can a second thread make the same call before the first one calls ReleasePrimitiveArrayCritical()?
Please help.
Yes, you can call GetPrimitiveArrayCritical() from two concurrent threads. The function will not block, and your two threads will grant access to the underlying array to native code. But on the other hand, the function will do nothing to synchronize this access, i.e. if thread 1 changes the value at index 100, and thread 2 also changes the value at index 100, you don't know which will be chosen at the end.
If you don't write to the array, you are guaranteed to be served correctly. Don't forget to ReleasePrimitiveArrayCritical with JNI_ABORT flag.
If you want to write to the array, check the output parameter isCopy as set by GetPrimitiveArrayCritical(JNIEnv *env, jarray array, jboolean *isCopy). If the result is 0, you can safely proceed with your multithreaded approach.
If the result is not 0, ReleasePrimitiveArrayCritical() will overwrite all elements of the Java array, even if some of them was changed in Java or in C on a different thread. If your program detects this situation, it must release the array (with JNI_ABORT) and wait for the other thread to complete. On Android I have never seen an array being copied, they are always locked in-place. But nobody will guarantee that this will not happen to you, either in a current system or in a future version.
That's why you MUST check isCopy parameter.

Categories

Resources