How to get performance increases using Processing for Android? - java

I have converted a few of my Processing sketches into Android apps, but they seem to run really slowly in the emulator and on my device.
Are there any tips on how to increase the speed and performance of my sketch running as an Android app? Are there things or parts of the Processing API I should avoid?

http://developer.android.com/guide/practices/design/performance.html
I hope that helps you.

Facepalm to all answers...Their answers are for programmers that need every single % of the CPU optimized. But I have the same issue as you, not really having much going on in the sketch, but it's still slow. And I doubt BlackDragon even knows what Processing is
My answer:
Try using a different renderer,
P2D for example.
You can use it by putting it next to the sketch size definition:
size(Width,Heigth,P2D);
Or if you don't want to use that function, you can override the sketchRenderer by placing this method.
String sketchRenderer(){
return P2D;
}

Related

how to know the performance of part of code using android studio

I want to know the performance of little fragment of my code using android studio.
I am writing a small part of my code here to explain my question
params.rightMargin = (int) (getResources().getDimensionPixelOffset(R.dimen.rightAndLeftMargin));
params.leftMargin = (int) (getResources().getDimensionPixelOffset(R.dimen.rightAndLeftMargin));
alternatively these lines can also be written as:
int margin = getResources().getDimensionPixelOffset(R.dimen.rightAndLeftMargin);
params.rightMargin = margin;
params.leftMargin = margin;
so with the help of android studio IDE how to compare the performance(like memory uses, CPU load, execution time etc.) of these two codes.
NOTE: This is not the only case I have dozens of same cases therefore I would like to have general solution for all types of codes.
With Profiler built-in into Android Studio you can easily see what methods on what threads are being called in a selected time frame. Personally I recommend using a Flame Chart to see what operation takes most amount of time.
Keep in mind that having a profiler attached to your app's process slows it down significantly, so if certain method call took e.g. 1 second, in reality it will be way less.
I don't know android at all. But when writing code, and you have a computation to make, it is better to make it once, and reuse the result. In your example, the latter is better.
I assume that the resources are already in the memory of the process, so probably the footprint here is minor. It will be to create a new method in the stack for getReaources, and another one for getDimensionPixelOffset. Creating a local variable it much cheaper that that.
The footprint increases significantly if you are making IO operations, such as accessing files, or http actions. In those cases it is much better to declare a local variable and reuse it.

is numerous class for specific functions good?

my main activity has over 400 Lines Of Code and contains numerous methods which deals with pdf generations, making the page dynamic, and other condition checks..
i was wondering if i should make it all into different class files.. each specific for its task.. thereby creating at least 3-4 different classes..
so my query is
1) will this approach make the app faster..
2) will this increase the app's size drastically??
also is there anyway to reduce the app's size?
i have deleted all unnecessary pics, xml's & assests..
i just want the size to be below 5mb..
Thanks in advance..
1) No. This won't noticably affect the app's speed.
2) No. The compiled code is actually the smallest part of an APK. Most of the size is caused by resources.
A way to reduce your app's size would be to optimize your compression on the images that are contained in your app. Also, you might be able to draw some of the images in code, as primitives such as lines, circles, squares, depending on what's on the images.
These points may help you :
Java class files increase the app size which you can ignore because it is very very less. So that point you no need to be worried.
As you have mentioned you deleted images and all. Those things actually increase the app size. So delete those as many as possible is not being used.
Creating more classes which will be specific for their purposes, that is OOPs concept and it is very much recommended. So if you want to modify something in future iy will be easy for you find the code as well your code will be modified in that particular class made for that sole pupose.
Hope it will help.
1) will this approach make the app faster?
Faster, can't comment. However, That approach will provide you an opportunity to learn how to make good design and will help you in future maintainability and extension of your project.
2) will this increase the app's size drastically?
Not at all.

What are the capabilities of canvas vs openGL?

I know that openGL works wonders if you send textures to it that are static and rarely change for example like tiles. But not when you have constantly changing sprites?
Is it possible to create games like abduction purely from canvas and what would be its performance?
It is possible to create games like abduction using canvas, however eventually you are going to hit a stumbling block in terms of performance.
OpenGL whether moving or static will handle images exponentially faster, by accessing buffers and pixel processors on the gc capable of manipulating large arrays of pixels at once.
However OpenGL isn't easy it will take time to learn, and you will need to learn it's language. This said you will find tons of information on using openGL, I highly recommend the Lightweight Java Game Library (LWJGL) http://lwjgl.org/ and NeHe tutorials http://nehe.gamedev.net/.
Anyway take a look see what you think, it'll be hard but as with all hard work it'll pay off eventually.
Hope this helps.

Timer in Java for noise generation

I am actually working on Android Call block App. however my part of code requires Java to generate obstacle/noise in the phone call.
I want to generate mute and unmute signals in a loop with varying intervals between them. Let me show with Pseudo Code in order to quickly explain logic.
array Tmute=[1,1,1,3,1,1,1,1,1]; //time in seconds to mute microphone
array TunMute=[3,1,2,4,1,2,1,2,4]; //time in seconds to unmute microphone
int i=0;
runThisLoop for (UserDefinedTime t)
{
MuteTheMicroPhone();
thread.sleep(Tmute[i]);
UnMuteTheMicroPhone();
thread.sleep(TunMute[i]);
i++;
}
DropCall();
I will take care of array out of bounds.
My Question : Is there a better way to generate the specific Tmute and TunMute which matches my pattern of noise generation, my pattern is shown in Array. Since I am using Android Device memory, I was concerned about efficient programming. Please comment and let me know if this question is not clear.
Is there a better way to generate the specific Tmute and TunMute which matches my pattern of noise generation.
Hard to answer without knowing why you think your current approach is poor. It looks OK to me; it is simple, it (apparently) does the job, what is the problem?
Since I am using Android Device memory, I was concerned about efficient programming.
I don't see any direct memory usage efficiency issues here. Unless perhaps turning the microphone on/off generates garbage.
But I'd be a bit concerned that turning the microphone on and off repeatedly might damage the hardware. If you are doing this simply to create a source of audio noise, surely there must be a technically better way to achieve that. Like a simple noise generator algorithm driven off a pseudo-random number generator.
And I'd also be a bit concerned about whether you have the real interests of the user of your app at heart. What are you really trying to do here?

Volume Control in Processing (java)?

I'm working on a Processing project. I tried to set the Volume of the audio played with the minim library. (setVolume seems not to be supported, and setGain has no effect)
So, I'm wondering whether there is no other, simpler way to control the audio in Processing? Or are there some Java commands, that can be used directly?
(In fact, I want to fade In and Out a short audio file, which means, I go through a loop and increase the volume after some steps)
I thank for every help!
Greetings
Nicolas
Try using Sonia or Beads (both listed here). Both of these will let you do what you're after.
I've had nothing but problems with Minim since it was first rolled into Processing; the other libraries are better documented, easier to use and a lot more stable!
I did it now with ESS.
ESS is perfect, because you have also a pause() and resume() function. And it works with MP3's.
thanks for your help!

Categories

Resources