When you pass an array into a function, then, as far as I know, you are really just passing the address of the first element to the function. This is cool because it means that the function is called super quickly since not all elements in the array must be copied.
I was wondering how a Hashmap is passed to a function. Does the compiler make a copy of the entire Hashmap, looping over all elements in the process? Or does the compiler see the Hashmap as an array plus the Hash function, i.e. one only passes the address of the first element of the underlying array? Or does something entirely different happen?
I made some quick tests and it seems that when I call the Hashmap in a function it is about 5% slower than if I do the same operations directly. That's not much so I guess it works like an array but I am not sure.
Edit: I messed up the test a little. Now it seems to take about twice the time
Related
My use case is this: a function called 'time' that will return how long it takes to run any function you give it.
So the time function needs to know all the parameters to pass into the function when it calls it.
I know how to pass a function into another function, but how can I pass all its parameters, without knowing in advance how many and what type they are, so they can be used when calling the function?
For example, if I pass in an array of all the parameters I need to send, is there some Dart way to call a function by expanding an array into a list of parameters? Or perhaps there's another way to capture and pass a function call, including all parameters, as one executable object?
I'm also interested in knowing if there's a more Dartful way to accomplish what I'm trying to do re: timing function calls.
I believe using a List of parameters with the apply method is the most common way and practical of doing this and I have seen something similar used to pass parameters for JS interop. As far as I know, there isn't a way to expand an array into a list of parameters like you can for javascript. You could of course create your own object to pass arguments, but I think it would add unnecessary complexity and end up being more difficult.
Example of passing parameters to function in dart:js here.
Is there any solid way to pass parameter to proc by reference(without using upvar)?
No. Not if you want to be able to modify the value inside the procedure and have that modification visible outside.
Tcl's procedures have their arguments passed strictly by value (the implementation actually passes by immutable reference, which is cheaper for complex structures, and uses a copy-on-write modification technique under the covers; it looks like pass-by-value) so you can't affect the outside world except by bringing it in with something like upvar. Sometimes, the things you pass in might be a handle to a modifiable entry (e.g., a class instance, a DOM node) and then you'd be able to observe a difference from outside, but that's not actually modifying the thing that was passed in itself (the handle) but rather the thing that it references.
From the performance point of view, which function definition is better/faster? Making an object and adding functions to that, or making functions one by one?
var myCollection:Object = {
first: function(variable:int):void {
},
second: function(variable:int):void {
}
}
myCollection.first(1);
or
private function first(variable:int):void {
}
private function second(variable:int):void {
}
first(1);
The latter. The performance hit will be negligible, except on a massive scale, but the second one will be slightly faster.
Basically it boils down to scope. To get a function from an object, you have to find the memory reference to the object within the scope of the class and then find the memory reference to the function within the scope of the object. With the second, you just have to find the Function object (all functions are objects) memory reference within the scope of the class.
The second method cuts out the middle man, essentially, in identifying the correct function. Now, each one will be found in less than a millisecond. As far as you are concerned, it is instant. But if you are doing this 100k times in a row? Yeah, you might see a bit of a performance boost by declaring within the class.
As an additional note, you are also adding another object to memory with the first one. Add enough of these (again, needs to be on a massive scale), and you will see a slowdown just from the superfluous objects stored in memory
You should also look at usability and readability, though. Declaring in an object means that the functions are not available as soon as the class is instantiated so you have to be careful you don't call the function before the object is instantiated. Additionally, you would lose code hinting and it is not the common way to write your code (meaning another dev, or even yourself a year from now, would have to figure out how it is working without any help from a hinter or from the standards they have already learned before they could do any modifications)
What does a period with a name before a function mean when calling it in Arduino code (C/C++)?
For example, I am using an OLED display library and one function is called like this:
display.setTextSize(1);
I know what this function does, but what does the syntax mean where there is some variable "display" or something before it?
In other words, why is a function called this way versus a normal call with just the function name and input?
"display" is an instance of an object, or a reference to some global/system variable. The "setTextSize" method is a member of that object. The end result means that you are setting the text size of, or on, "display".
This lets you do things more concisely by being able to say display.setTextSize(1), foo.setTextSize(1) and bar.setTextSize(1) without having to specify unique functions for each different item on which you are setting the text size.
Within setTextSize you will probably see "this". "this" in only this one instance means "display". If you used bar.setTextSize(1), "this" would mean "bar" and so on.
I could be incredibly wrong, but I think its got to do with structures. In the arduino environment there's a few different functions that revolve around using serial communication. They have it set up as a library that gets called on whenever you use Serial.something();
The something could be any of the functions that is part of serial, like Serial.read();
EDIT forgot to put a source in. http://arduino.cc/en/Reference/Serial
Apologies if I'm way off, still new at this, and also can't figure out how to just make a comment.
I have created a class that I've been using as the storage for all listings in my applications. The class allows me to "sign" an object to a listing (which can be created on the fly via the sign() method like so):
manager.sign(myObject, "someList");
This stores the index of the element (using it's unique id) in the newly created or previously created listing "someList" as well as the object in a 2D array. So for example, I might end up with this:
trace(_indexes["someList"][objectId]); // 0 - the object is the first in this list
trace(_instances["someList"]); // [object MyObject]
The class has another two methods:
find(signature:String):Array
This method returns an array via slice() containing all of the elements signed with the given signature.
findFirst(signature:String):Object
This method just returns the first object in a given listing
So to retrieve myObject I can either go:
trace(find("someList")[0]); or trace(findFirst("someList"));
Finally, there is an unsign() function which will remove an object from a given listing. This function basically:
Stores the result of pop() in the specified listing against a variable.
Uses the stored index to quickly replace the specified object with the pop()'d item.
Deletes the stored index for the specified object and updates the index for the pop()'d item.
Through all this, using unsign() will remove an object extremely quickly from a listing of any size.
Now this is all well and good, but I've had some thoughts which are making me consider how good this really is? I mean being able to easily list, remove and access lists of anything I want throughout the application like this is awesome - but is there a catch?
A couple of starting thoughts I have had are:
So far I haven't implemented support for listings that are private and only accessible via a given class.
Memory - this doesn't seem very memory efficient. Then again, neither is creating arrays for everything I want to store individually either. Just seems.. Larger.. Somehow.
Any insights?
I've uploaded the class here in case the above doesn't make much sense: https://projectavian.com/AviManager.as
Your solution seems pretty solid. If you're looking to modify it to be a bit more extensible and handle rights management, you might consider moving all those individually indexed properties to a value object for your AV elements. You could perform operations like "sign" and "unsign" internally in the VOs, or check for access rights. Your management class could monitor the collection of these VOs, pass them around, perform the method calls, and the objects would hold the state in a bit more readable format.
Really, though, this is entering into a coding style discussion. Your method works and it's not particularly inefficient. Just make sure the code is readable, encapsulated, and extensible and you're good.