patternMinor
Binary/yijing clock in Processing with excessive resource consumption
Viewed 0 times
excessiveyijingwithresourcebinaryclockprocessingconsumption
Problem
I've been learning Processing for the last week or two, and I've made a couple of little 'desktop toy' type apps like this one that I'm quite pleased with. However, it just occurred to me today to look at the activity monitor while my app was running, and I was unpleasantly surprised – nobody wants a desktop toy that's consuming more resources than anything else on the system.
So far throughout my coding education/experience I've been told not to worry about optimisation 'yet', and haven't really been offered much guidance re: efficient coding (faculty are mainly interested in code that's easy to mark).
Here's what the app is presenting to the user:
Versus the resources it's consuming:
The source is in two parts: the main .pde and another containing the 'Gua' (hexagram) class.
yijingClock.pde
```
PGraphics mins;
PGraphics hrs;
float fadeAmount;
static final float fadeMax = 1440; //1440 means 1 step per frame takes 1 minute at 24fps
void setup() {
size(500, 500);
colorMode(RGB, 255, 255, 255, fadeMax);
background(255);
imageMode(CENTER); // All images in the sketch are drawn CENTER-wise
frameRate(24); // Affects the smoothness and speed of fade();
mins = createGraphics(width, height);
hrs = createGraphics(width, height);
noFill();
stroke(100);
//polygon(5, width/2, height/2, width / 4 3, height / 4 3, -PI / 2); // Draw a static pentagon as the background
fill(0);
fadeAmount = 0;
} // end setup()
void draw() {
//let's fade instead of redrawing the background to expose change over time
fadeAmount = map(System.currentTimeMillis() % 60000, 0, 60000, 1, fadeMax); // new way explicitly ties the fade amount to the real current second
//println(fadeAmount);
fade(fadeAmount);
drawMins();
drawHrs();
}// end draw()
void drawMins() {
Gua gua = new Gua(minute());
mins = gua.drawGua(color(0, 0, 0, constrain(fadeAmount*2, 100, fadeMax)));
image(mins, width/2.0, height/2.0, width/2.5, height/2.5);
}// end drawMins()
vo
So far throughout my coding education/experience I've been told not to worry about optimisation 'yet', and haven't really been offered much guidance re: efficient coding (faculty are mainly interested in code that's easy to mark).
Here's what the app is presenting to the user:
Versus the resources it's consuming:
The source is in two parts: the main .pde and another containing the 'Gua' (hexagram) class.
yijingClock.pde
```
PGraphics mins;
PGraphics hrs;
float fadeAmount;
static final float fadeMax = 1440; //1440 means 1 step per frame takes 1 minute at 24fps
void setup() {
size(500, 500);
colorMode(RGB, 255, 255, 255, fadeMax);
background(255);
imageMode(CENTER); // All images in the sketch are drawn CENTER-wise
frameRate(24); // Affects the smoothness and speed of fade();
mins = createGraphics(width, height);
hrs = createGraphics(width, height);
noFill();
stroke(100);
//polygon(5, width/2, height/2, width / 4 3, height / 4 3, -PI / 2); // Draw a static pentagon as the background
fill(0);
fadeAmount = 0;
} // end setup()
void draw() {
//let's fade instead of redrawing the background to expose change over time
fadeAmount = map(System.currentTimeMillis() % 60000, 0, 60000, 1, fadeMax); // new way explicitly ties the fade amount to the real current second
//println(fadeAmount);
fade(fadeAmount);
drawMins();
drawHrs();
}// end draw()
void drawMins() {
Gua gua = new Gua(minute());
mins = gua.drawGua(color(0, 0, 0, constrain(fadeAmount*2, 100, fadeMax)));
image(mins, width/2.0, height/2.0, width/2.5, height/2.5);
}// end drawMins()
vo
Solution
Something you need to learn about performance in java is that if performance is important to you, Java might not be the language for you. Your code is not directly translated into machine code; the compiler interprets your code, rewrites it with it's own optimization algorithms before translating it to machine code. Of course you can make minor changes to your code to make slight performance improvements, but on a general scale going for performance means it becomes more of a game of "how to trick the compiler" rather than pure performance increase through code.
To your actual code
Comments
You're commenting way too much. If this is for your own learning process and noone else have to look at the code, that's fine, but if this is code you are gonna share, commenting near every line is only gonna make it hard to read quite annoying. Anyone who didn't pick up coding 2 weeks ago don't need to see
JavaDoc are actually quite useful if you do them right;
If you're using an IDE like NetBeans or IntelliJ this JavaDoc will pop up and let you know what the method does and how to use it (just like it will help anyone else using your code). However, you're not writing JavaDoc, you're writing block comments that explains the code, not documents it.
You're also leaving a lot of scrap comments in your code. If you have to comment it out and it's no longer a WIP, remove unused code.
Access modifiers
It's also bad practice to not add access control modifiers, if it's something you haven't studied yet you can read about it in the oracle tutorial.
Magic numbers and hard-coding
You should also try to not hard-code everything. There's something called "magic numbers" and you're using them near everywhere. Ins this for loop you have three:
The first one can be somewhat forgiven since index begins with 0 and there might not be any places you want to modify this, but the other two;
Why it's important to not use magic numbers is that a) You'll know what the number represents and it's easier to follow what you're iterating. b) The most important thing! If you need to change these numbers, you only change one variable declared at the top in the global scope, otherwise you need to somehow know and remember which magic numbers are related to this variable and has to be changed as well.
And when you hard-code all the variables locally, your methods are not dynamic and can't be re-used. It might not seem to matter here, but it's a good practice to put in among your habits when coding.
To your actual code
Comments
You're commenting way too much. If this is for your own learning process and noone else have to look at the code, that's fine, but if this is code you are gonna share, commenting near every line is only gonna make it hard to read quite annoying. Anyone who didn't pick up coding 2 weeks ago don't need to see
// end methodName(). You should only write comments when your code does something that's not obvious just by reading it. You're not making a tutorial for people who can't read code, you're writing comments to clarify difficult parts to yourself and your colleagues.JavaDoc are actually quite useful if you do them right;
/**
* This method connects to the database and persists the ObjectType entity provided.
* @param ObjectType
* @return ObjectType (persisted object)
*/If you're using an IDE like NetBeans or IntelliJ this JavaDoc will pop up and let you know what the method does and how to use it (just like it will help anyone else using your code). However, you're not writing JavaDoc, you're writing block comments that explains the code, not documents it.
You're also leaving a lot of scrap comments in your code. If you have to comment it out and it's no longer a WIP, remove unused code.
Access modifiers
It's also bad practice to not add access control modifiers, if it's something you haven't studied yet you can read about it in the oracle tutorial.
Magic numbers and hard-coding
You should also try to not hard-code everything. There's something called "magic numbers" and you're using them near everywhere. Ins this for loop you have three:
for (y = 0; y <= 250; y = y + 50) {}The first one can be somewhat forgiven since index begins with 0 and there might not be any places you want to modify this, but the other two;
- What is 250? Where does it come from? Why is it that specific number?
- y = y + 50 raises the same questions.
Why it's important to not use magic numbers is that a) You'll know what the number represents and it's easier to follow what you're iterating. b) The most important thing! If you need to change these numbers, you only change one variable declared at the top in the global scope, otherwise you need to somehow know and remember which magic numbers are related to this variable and has to be changed as well.
And when you hard-code all the variables locally, your methods are not dynamic and can't be re-used. It might not seem to matter here, but it's a good practice to put in among your habits when coding.
Code Snippets
/**
* This method connects to the database and persists the ObjectType entity provided.
* @param ObjectType
* @return ObjectType (persisted object)
*/for (y = 0; y <= 250; y = y + 50) {}Context
StackExchange Code Review Q#109439, answer score: 2
Revisions (0)
No revisions yet.