It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have many objects (such as 10.000, more or less). Every object has3 values :
Index (decimal, such as 0,0 <= X <= 100.000,9);
A Latitude value;
A Longitude value;
and I need to perform some search due to the Index value. Which will be the light approch to this? List<MyObject>? I know there are hashtable, but only for 2 values...
I read these values from a .csv file and I'll store it on application. WebForm, .NET 4.5.
The very lightest approach in terms of memory use is to put these into a struct, and hold them in an array of such structs. From what you say, you can't really pack data any tighter than that: two doubles and a decimal will occupy 32 bytes per entry, and the array of structs does not add any per-item overhead on top of this.
Having said that, this will slow down your coding and might save too little to matter in practice.
Why don't you use a Dictionary like this:
public class Position
{
public Latitude Latitude { get ; set ; }
public Longitude Longitude { get ; set ; }
}
public Dictionary<decimal,Position> Positions ;
Or use a Tuple in the dictionary:
public Dictionary<decimal,Tuple<Latitude,Longitude>> Positions ;
I believe the absolutely lightest approach would be bitmasking your values into an unsigned long, though it is slightly cumbersome.
To really get the grasp on which is the most efficient approach, i recommend trying them all with test values and looking at the output of sizeof() on them. that way you'd be really sure what's their runtime memory size.
I'd suggest a custom struct to hold your values, a tuple could work as well.
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I hate to ask this question on here, but I have searched both SO and Google to no success. I have seen in many places statements such as while(var != -1) and other statements, often loops, containing some sort of reference to -1. Is there a certain meaning to the use of -1, or is it just used as giving an integer representation of a boolean, or something like that? I have mainly seen this in C# programming if that is any help.
in C# -1 is just negative one. They're comparing a number against a number, seeing if it is indeed equal to negative one.
It's not uncommon to have an integer field that should only have positive values (for example, when representing an index in a list) and in such cases -1 is sometimes used to represent "not a valid value", for example, there is no item, and hence no index. They use -1 because an int is not nullable; they cannot assign null.
In theory this is probably a bad practice; it's using a "magic value" to mean something more than it really should. Ideally if "there is not valid" is a valid thing for the variable to represent it should be a nullable integer (int? or Nullable<int>) but this is an old convention (carried over from other languages without a feature for nullable ints) so it's hard to eliminate entirely.
Nothing special about it. It's just that in most frameworks and libraries, functions or methods that return an index of an element in a collection will return -1 when whatever you're looking for isn't in the collection.
For example, the index of the character b in the string foo would be -1 in JavaScript, .NET and, as far as I remember, Java as well.
So many devs have burned a rom in their minds saying that -1 is the index for not found items. Now you know why.
If you know that an int should always contain positive value (for instance an item count or an index in a list, -1 can be a kind of "reserved value", so you would for instance assign the count to -1 and as long as it's -1, you know no real value has been put in there, a bit like a "null"
other than that I don't think there's any special meaning to -1
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am trying to solve the Project Euler problem number 6 with the following program:
double counter = 1;
double sumsquare = 0;
double squaresum = 0;
while (counter <= 100)
{
sumsquare += Math.Pow(counter, 2);
squaresum += counter;
counter++;
}
Math.Pow(squaresum, 2);
Console.WriteLine("the sum is {0} ,the square is :{1}", squaresum.ToString(), sumsquare.ToString());
double diff = sumsquare - squaresum;
Console.WriteLine(diff.ToString());
Console.ReadLine();
However, the answer was wrong. Can anyone tell me what the problem is with my code?
You have an error in that you are computing a power and then discarding it.
More generally, you should avoid double when solving PE problems that involve integers. Doubles are only accurate to fifteen decimal places; after that, they round off.
Better types to use are long, which can represent integers up to 9,223,372,036,854,775,807 or decimal, which can represent integers up to 79,228,162,514,264,337,593,543,950,335, or BigInteger, which can represent arbitrarily large values.
Note that code typically gets slower as you use the larger and larger types.
Even more generally, now would be a good time to learn how to use a debugger. Figure out what the output of your program should be for a very small case that you can calculate by hand. Then step through your program line by line and see where it goes wrong. That is a far more efficient way to debug a problem than asking the internet.
You seem to think that Math.Pow(squaresum, 2); modifies squaresum. That's not so: it returns a square of squaresum, and you should assign it to squaresum if you want the variable to be modified.
squaresum = Math.Pow(squaresum, 2);
Also the idea of using double type here is not quite good, but it appears that you won't lose enough precision here to get a wrong result.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am looking to generate programmaticaly a list of concrete nouns, an associated picture and if possible a sentence describing the proper noun.
I have tried various dictionary APIs - but the first part of the problem - getting a list of concrete nouns has caused me difficulty. Can anybody think of a good way of achieving this I would be very interested in hearing about it!
Please be aware I know what hashmaps are - and storing this data is not my problem - more sourcing the data is what I need help with - WHERE do I get a list of concrete nouns I can progrmmatically iterate over.
Cheers
NLTK has a part of speech tagger. You could run it on a piece of text and store all the nouns it identifies as your list.
If you want a list of all nouns, you might be in for a long hunt - you'd have to run through every dictionary, encyclopedia, atlas and baby names book in the English language. A more reasonable place to start would be this list of 2336 nouns of various kinds. They reckon it's short of a complete list by 50,000 or so - and my bet is that's an underestimate.
If you want to do in Java
You can use HashMap to store the data; where key can be proper noun and value an object which has other details
HashMap<String, ProperNounObj> obj = new HashMap<String, ProperNounObj>();
where ProperNounObj class has attributes like picutureUrl and description
List of proper noun can be generated by hashmap method obj.keySet(); this will return a set of all proper noun.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I ask this question out of sheer curiosity. I don't have any actual code to ponder over.
Is there a best-case scenario for using a jagged array instead of a list of lists, and vice versa?
a List<T> is an array, just a wrapped in a class so it can reallocate and resize it at will. So otherwise performance between the two can be considered identical - only use List<List<T>> if you need to resize either dimension - don't forget that whenever you resize the 0th dimension you'll need to construct new instances in the 1st dimension, which can be expensive.
You forgot n-dimensional arrays (i.e. T[,]). However, due to a quirk in .NET's bounds-checking these are actually slower than jagged arrays or single-dimensional-user-managed arrays, which is a complete mystery.
Jagged arrays and lists of lists have the same relative merits as plain lists and arrays:
The main advantage of a List<T> is that you can grow and shrink it. However it may take up a lot of extra space in memory because it generally keeps a bunch of extra space in its backing store so that it doesn't have to re-allocate a new array every single time it grows.
The main advantage of an array is that it's compact - it has exactly enough space to store the number of elements that are in it, and no more. But its size is static; if you need to add a new item to it you must either replace an existing item or manually create a bigger array and copy data into it.
In terms of speed they should behave identically, to any practical extent. List<T> uses an array for its backing store, so it might be slightly slower than an equivalent array because of the extra layer of indirection. But I wouldn't assume that without careful measurements - whether and to what extent it does is the kind of thing that could change with different versions of the framework or the CLR.
I would say a List<T> is more flexible where as a T[] is more inter-operable. For example, if I wanted to construct a list of an unknown size, I would use a List<T>. If I was returning a list from a web-service I would use a T[].
I know I am referring to single-dimension lists here, but the same practice would apply for multiple dimensions.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I need an array which will only contain the values 0 and 1. Will bool[] be good enough for me? Or is there something lighter weight?
EDIT:
I dont have memory constraints but that array is made and passes online All The Time with big files passes concurrently with that array. I want the maximun optimization so the big files wont be delayed
A bool is probably not the best way to do it. Depends how many numbers you have got.
It is important to realise that even though a bool is a single bit, it requires a full byte in memory.
A BitArray on the other hand takes care of this for you and is more space efficient, although ever so slightly less time efficient.
http://msdn.microsoft.com/en-us/library/system.collections.bitarray.aspx
Depends on your constraints, if it is not for a constrained environment a bool array will work just fine.
Bool Array is good enough. You can consider "false" as 0 and "true" as 1.
May be you need BitArray, the sequence of 1 and 0.
bool[] would do the trick... If your 0 & 1 numbers are in fact just "Flags" and not real numbers.
Using an Enum with the Flags attribute is another option. This would allow you to have an Intention Revealing Name for both boolean values.
[Flags()]
public enum TheFlags
{
NoneSet = 0,
FirstSet = 1,
SecondSet = 2
}
Then you can check if "First" is set like so:
TheFlags flags = TheFlags.FirstSet;
if (flags.HasFlag(TheFlags.FirstSet))
Console.WriteLine("First flag is set!");