Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 29 May 2008
Offline Last Active Apr 01 2013 03:27 PM

#4999305 The Singleton Pattern: To be or not to be [used]?

Posted by Caburé on 09 November 2012 - 09:02 AM


There are many times when a program needs to store and reference data that is a result of processing and that can be used again. How will you store metadata? I like to use singletons for structural/constant data/flags/metadata(temporary) instances, when the complexity of creating new instances and having it loading its data from something/somewhere doesn't worth as simply acessing it straight like cache of processors is used and Windows Registry (always in memory).

sorry it doesn't float at all.
It doesn't have to be a ONE! You create it ONCE, and pass it around or publish it to objects interested in it.
You are talking about trading memory for computation costs.. it doesn't have anything to do with SINGLENESS and globality of scope.

Singleton is single instance. It does not necessarily mean global scope (you can create a singleton in the scope of a package, for example).
Singleton is static - that is why there is a static word in Java (and not, again, necessarily, a global pointer/variable).
If you could reraise the data of a singleton just by making a new instance without costs, what would be the reason to use static classes (singleton) - that is what I am explaining?

How would you store, for example, trigonometry tables? If you can't see that, every time you declare a public static final int, the class where you declared it becomes a singleton, conceptually: is accessible in global scope and has only one static instance - the class itself. In Java, you can even set/change static variables at runtime (but not final variables).

I just said how I like to use SingleTons : to store metadata. I didn't argue that it is the only way to do that. But.. ppl seem to be stuck in just one technique.

After all, I am posting in the wrong place, I forgot to tell that I was meaning Java as programming language.

#4998046 How powerful is Java?

Posted by Caburé on 06 November 2012 - 09:07 AM

Java offers ways to process pixels fast using BufferedImage. I've made this sample to test it (2D):

There is just one character and effects made straight to the pixels. I believe that, if things should have been spread through other objects, it could need a redesign to take advantage of architetural aspects as how loops and other predictions and metadata creation are made.

#4997679 How powerful is Java?

Posted by Caburé on 05 November 2012 - 01:06 PM

Many functions of Java rely on the utilization of API that are not specilalized (like using BufferedImages and 2D GUI Api to draw sprites). Depending on which API you will use, your game may consume many more memory than necessary and go much slower than you would expect. The compiler+language+architeture are not a deal but how they are binded with some technologies(API) makes a HUGE difference.

I don't like to be forced to use (b & 0xff) for every byte to be able to know its integer value (to the hardware it donesn't need that operation but whenever the data need to be known or parsed it will have to be translated to more common values, if you like to use more known constants).

Data are not data.. you can't just cast things because they are encapsulated in round/closed solutions called classes... and they require you to use paths to deal with them: functions.. and java functions can be tedious since you may lose speed if you are heavy user of objects..

Java is not for speed. Is for portability and facilities(web). What you see as graphical results comparing Java and C/C++ is just the work of native graphics API/drivers receiving the necessary data at a constant rate. And the data received by OpenGL, for example, must match its requirements and I don't know if the java interface doesn't shift things before sending to the drivers.

Memory: you don't know how much memory an object will take and how worse it can get when you use third-party objects made for other kinds of solutions (not specialized in your case/solution) when you just use a bunch of them (Java is good using arraylists to store objects, not arrays - as records/structures..). In Java, all turns to facilitate the alignment in memory: I believe that most of the speed Java has is because the compiler hides what he does do store your, for example, stricted 3 byte data in 4 bytes in memory, wasting one byte just to have easy access, and how big is the speculation of the amount of memory needed for your JVM when you allocate memory: if you read a 30MB file, the JVM will request more than 80MB to the OS, trying to facilitate things for the programmer by speculation to the "future".. the flawless impression that Java does costs memory because when there is no more memory, your virtual machine/application just stalls and you have nothing more to do (even freeing objects won't make you able to continue, by how I use to face).
There is the garbage collector but it is more to a unspecialized guardian of presumable roads through what you may go or not, trying to let out of the oval circuit of memory reutilization the "stones" that your last lap had left and that you are maybe still using... lost threads sometimes are part of those.. but it is not easy to have a virtual clean and opened highway in front of you when you don't have ways to know in what speed you'll go (I mean dynamic memory utilization) because only the JVM knows where to store what you need.. and aligned in 4 bytes.. always.. per object (if you have a thousand objects of 3 bytes, you'll lose a thousand bytes if you store it in objects - if it is good or not, is up to you to know).

You may notice that if the project made in Java acts like a known tree, having all its growth process presumed and repeating it everytime, the JVM will not have so many issues about memory or the lack of speed that some facilitations that high level languages offer and that it is up to the programmer to make code that will slimly fit into the sharp requisites of API, working perfectly in the electric level of seam between the software conceptions and its overheads and the hardware immediate needs and characteristics but.. take care about what Java hides..

#4994861 The maths behind the rotations

Posted by Caburé on 28 October 2012 - 04:59 PM

This answer is not specifically to the parner who made the question. Might even be for myself.

Man, the hardware just makes an illusion, after all. Don't get nuts about that or about how hard it is to grip the source of an specific equation/function as if you had to imagine it based on its results instead of how it works in a specific case, binded for a purpose (math alone is steryle but is amazing when joins its functions in a purpose to work as an analogy of the nature - in this case, resulting graphical analogy in heterogeneous equations ).

What those funcions have worthing (trigonometric) is that they return something between a range in a fixed way (what comes x goes out y, always), bouncing from up to down (or left/right, back/forth, -1..1, sun and moon, black to white, whatever defined and represented as a - mostly required smooth- transition).

What you see in the screen is a composition resulting from a specific time changing parameters that result in a scene created ( I am not going out of the scope since the "bones" of the issue ARE the mathematical functions). A simulation as how hardware works is no magic but a bunch of tricks and those trigonometric functions are nothing but this:


ACTUALLY a bounce. ACTUALLY. Other kinds of functions represent (because they act as) other natural phenomenons (as a steady grow in 2 dimensions done by a square or three dimension done by a cube), going parallel (as simulation) to what happens in nature, chained to other functions based on natural results of natural interactions of the aspect (variable) you are seeking for. That is directed/aimed functionality (with functions being its "artifacts", not its purpose - despite being a purpose as language to computers).

You may not grip the math as a plotted dropping results of specific variables flowing thorugh functions to define spatial conditions because it renders too fast. If you could see the rendering not as fast, you could then see how the interactions lead the results into known ways of using trigonometry to simulate movement (riding a bicycle is somewhat like - you know the steps but speed makes it work). Don't fall into the functions as if they were an "identity of a transformation" by its declaration but take it slow to see that every piece of a function is an independent possibility bounded specifically to act as a part of a simulation of phenomenon either in literal meanings (direct rendering) or flow control (function direction/parameter). What you see in an algebric expression is an articulation or an identity to a natural event at last, in its minimal aspect known/identifiable.

There is no physical limit for simulation. Don't get functions as fetish without understanding its limits and how they are connected and the pratical purpose.

The results you see is just the attenuations/vectorizations(perspective adjustments) of the same functions based on another variations, all computed in a way to APPEAR that there is a WORLD being drawn and not just "chained reactions of known results based on observed motions in reality that goes in just ONE DIMENSION for EACH VARIABLE, positioned in a 2 dimensional space. 2 (even for 3D graphics).". Don't grip the world, the simulation is PURE ARTIFACT of reality, made by mind. Don't go believing that there is a magic for "deep simulation" in the functions because there is no deep, just a parameter changing how a variable changes based on a simbology that turns a number into a fraction of another and THAT IS THE MAGIC: A number changing others based on its own value and by that change in especific and isolated variables, shifting up/down, reducing lengths of lines, affecting sizes, TURNING/TREATING X into/as Z, Z into/as Y, Y into/as X and causing the effect of rotation/flipping based on simple substitution (each matrix used can use anything as anything, by any parameters based on any source - anything in this case is NUMBERS that can be vectors/scalars or those "normals" that put z into z, x into x and y into y again to close the circus). I swear again: THERE IS NO REAL TRANSLATION, DEPTH, SCALE, ROTATION or whatever: what you see is a number changing another number to appear a scene, all based on an immitation of what would happen in reality (the "world" is just a relation between functions: no matter how beautiful or weird an equation/function may appear because it doesn't work by itself and if the real condition from where it was concepted changes, the equation might change too - the conception of a function is the real art/synthesis, its utilization as graphics is, amazingly, just artifact of math - in analogy to nature).

If algebra is like the sun for you, don't look at the sun. See how it was made and you might understand (the many aspects of reality that math simulates and how they join to result in a variable).

Your vision shall not be the same as the Marionette (I hope) but the handler (who has nothing more than a few strings and sticks but, by his hability, can make a beautiful world to entertain people - math just have numbers and its transformations.. single or chained functions based on nature, or not):


Sorry for my way of explaining, might not be good but, may help.