efficiency

Java Concurrency: lock effiency

Hello My program has 100 threads. Every single thread does this: 1) if arrayList is empty, add element with certain properties to it 2) if arrayList is not empty, iterate through elements found in arrayList, if found suitable element (matching certain properties), get it and remove the arrayList The problem here is that while one th...

Array, Dictionary or List in a session?

Hey, What would be the most efficient way of storing a set of values in a session? I'm guessing it's either a List/Array or Dictionary. Basically, when a user selects an item from a DropDownList, a value (between 1 and N where N <= 20) is sent back to the server. I'd then like this value to be used as an index (if using arrays) or key ...

Finding the range in which a number lies

Hi, I have the following method: private static final int OPEN_FOR_RAISE_RANGE = 35; private static final int OPEN_FOR_CALL_RANGE = 25; public void foo(double num){ if(num <= OPEN_FOR_RAISE_RANGE){ //do something } else if(num <= (OPEN_FOR_RAISE_RANGE + OPEN_FOR_CALL_RANGE)){ //do something else } ...

Efficient Python array with 100 million zeros?

What is an efficient way to initialize and access elements of a large array in Python? I want to create an array in Python with 100 million entries, unsigned 4-byte integers, initialized to zero. I want fast array access, preferably with contiguous memory. Strangely, NumPy arrays seem to be performing very slow. Are there alternatives ...

Data Structure for Storing Ranges...

I am wondering if anyone knows of a data structure which would efficiently handle the following situation: The data structure should store several, possibly overlapping, variable length ranges on some continuous timescale. For example, you might add the ranges a:[0,3], b:[4,7], c:[0,9]. Insertion time does not need to be particularly ...

PHP - To echo or not to echo?

What is more efficient and/or what is better practice. To echo the html or the have many open and close php tags? Obviously for big areas of html it is sensible to open and close the php tags. What about when dealing with something like generating XML... should you open and close the PHP tags with a single echo for each piece of data or...

Trees: Linked Lists vs Arrays (Efficiency)

This is an assignment question that I am having trouble wording an answer to. "Suppose a tree may have up to k children per node. Let v be the average number of children per node. For what value(s) of v is it more efficient (in terms of space used) to store the child nodes in a linked list versus storage in an array? Why?" I believe I ...

What's the best way to dedupe a table?

I've seen a couple of solutions for this, but I'm wondering what the best and most efficient way is to de-dupe a table. You can use code (SQL, etc.) to illustrate your point, but I'm just looking for basic algorithms. I assumed there would already be a question about this on SO, but I wasn't able to find one, so if it already exists just...

Most efficient way to find the greatest of three ints

(pseudo code) function highest(i, j, k) { if(i > j && i > k) { return i; } else if (j > k) { return j; } else { return k; } } I think that works, but is that the most efficient way? ...

Would it be better to have the info in both tables? Or create a third table?

I have one table GAMES and another PLAYERS. Currently each "game" has a column for players_in_game but I have nothing reciprocating in the PLAYERS table. Since this column is an array (Comma separated list of the player's ID #s) I'm thinking that it would probably be better to have each player's record also contain a list of the games th...

Vectorizing for loops in Matlab

I'm not too sure if this is possible, but my understanding of Matlab could certainly be better. I have some code I wish to vectorize as it's causing quite a bottleneck in my program. It's part of an optimisation routine which has many possible configurations of Short Term Average (STA), Long Term Average (LTA) and Sensitivity (OnSense) ...

Optimisation of Ruby algorithm for grouping and counting colours.

Hi, i have what seems on the surface a simple problem which i wish to solve using ruby, i have a bunch of colours with associated photo id's, e.g [[1,"red"],[1,"green"],[2,"red"],[3,"yellow"],[4,"green"],[4,"red"]] and i wish to process the data so that it is in this format: 2 photos for red,green 3 photos for red 1 photo for yellow ...

Efficiency of stack-based expression evaluation for math parsing

Hello everybody. I have to write, for academic purposes, an application that plots user-input expressions like: f(x) = 1 - exp(3^(5*ln(cosx)) + x) The approach I've chosen to write the parser is to convert the expression in RPN with the Shunting-Yard algorithm, treating primitive functions like "cos" as unary operators. This means the f...

how to efficiently get the k bigger elements of a list in python

Hi! What´s the most efficient, elegant and pythonic way of solving this problem? Given a list (or set or whatever) of n elements, we want to get the k biggest ones. ( You can assume k<n/2 without loss of generality, I guess) For example, if the list were: l = [9,1,6,4,2,8,3,7,5] n = 9, and let's say k = 3. What's the most efficient a...

Transaction support in an observable collection

I'm interested the most efficient way to change an observable collection in such a way that only one property changed is fired. Lets say that I want to populate the list with 3 items, there is no addCollection method or something like that, so I have to do clear + 3 times add. Do I need to create a different observable collection and ass...

Compare bit sets efficiently

I want to store permissions in a bit set to compare them efficiently in Java. A 1 would mean permission granted and 0 permission denied. If the authorization is performed the required permissions would be compared to the permission set. My idea is to use AND and compare the result with the requested permissions. Example: 0010 110...

Undirected Pair State

I'm looking for an "efficient" way to persist a binary state when given to two integers. Giving these two integers A an B, A is always less than B and the range of values which they will contain is 0 through N. The integer N will be greater than 2 and less than 256. The simple solution is to create a two-dimensional array of Boolean v...

Efficiency: What data structure to use...?

Hi developers! I am working with a very large dataset. Essentially I will be working with millions of records and storing a value into a dataset. Each time that I store a value, I must first check to make sure that the value is not already in the data structure. If the value is in the data structure, I must update (or remove/add) the...

Fastest way to get maximum value from an exclusive Range in ruby

Ok, so say you have a really big Range in ruby. I want to find a way to get the max value in the Range. The Range is exclusive (defined with three dots) meaning that it does not include the end object in it's results. It could be made up of Integer, String, Time, or really any object that responds to #<=> and #succ. (which are the only ...

Declaring/passing structs vs. declaring/passing individual values

I have two questions, actually. But they are very closely related. Suppose I have a simple struct like this: public struct TradePrice { public float Price; public int Quantity; public TradePrice(float price, int quantity) { Price = price; Quantity = quantity; } } Question 1 Now if I have code somewhe...