efficiency

Read multiple records given array of record IDs from the database efficiently

If you have an array of record ID's in your application code, what is the best way to read the records from the database? $idNumsIWant = {2,4,5,7,9,23,56}; Obviously looping over each ID is bad because you do n queries: foreach ($idNumsIWant as $memID) { $DBinfo = mysql_fetch_assoc(mysql_query("SELECT * FROM members WHERE mem_id ...

looping over char[] or substring(): Efficiency in C#?

Hello, does any of you know what would be better: a. get a string s, convert to char array and loop over it, or b. get a string s, loop over substrings of it (s.Substring(i, 1))? Any tips much appreciated. ...

Merge sort in Haskell

Hi, I am new to Haskell and I am trying to implement a few known algorithms in it. I have implemented merge sort on strings. I am a bit disappointed with the performance of my Haskell implementation compared to C and Java implementations. On my machine (Ubuntu Linux, 1.8 GHz), C (gcc 4.3.3) sorts 1 000 000 strings in 1.85 s, Java (Java...

Efficient Hashmap Use

What is the more efficient approach for using hashmaps? A) Use multiple smaller hashmaps, or B) store all objects in one giant hashmap? (Assume that the hashing algorithm for the keys is fairly efficient, resulting in few collisions) CLARIFICATION: Option B implies segregation by primary key -- i.e. no additional lookup is necess...

Validate DateTime's are sequential by month

I have a list containing 60 DateTime objects (sorted in ascending order) and need to validate that each date is 1 month greater than the previous one in the list. For example, the following list of dates would be valid because they increment by one month with none missing: Jan-2009 Feb-2009 Mar-2009 Apr-2009 However, the followi...

Most efficient way to reverse a stack and add to an ArrayList

So, this is an efficiency question. I have two collections - an ArrayList and a Stack. I use the stack because I needed some simple pop/push functionality for this bit of code. The ArrayList is essentially the out variable as this is a small section of code in the function. So, I the variables are defined as such, then code is run to ...

SQL Alternative to performing an INNER JOIN on a single table

I have a large table (TokenFrequency) which has millions of rows in it. The TokenFrequency table that is structured like this: Table - TokenFrequency id - int, primary key source - int, foreign key token - char count - int My goal is to select all of the rows in which two sources have the same token in it. For example if my table ...

How can I set the file-read buffer size in Perl to optimize it for large files?

I understand that both Java and Perl try quite hard to find a one-size-fits all default buffer size when reading in files, but I find their choices to be increasingly antiquated, and am having a problem changing the default choice when it comes to Perl. In the case of Perl, which I believe uses 8K buffers by default, similar to Java's ...

Which algorithm(s) can solve this constraint programming problem ?

Hello, I need to solve a job affectation problem and I would like to find preferably efficient algorithms to solve this problem. Let's say there are some workers that can do several kind of tasks. We also have a pool of tasks which must be done every week. Each task takes some time. Each task must be taken by someone. Each worker must...

Regex Performance Optimization Tips and Tricks

After reading a pretty good article on regex optimization in java I was wondering what are the other good tips for creating fast and efficient regular expressions? ...

Efficient container for bits

I have a bit array that can be very dense in some parts and very sparse in others. The array can get as large as 2**32 bits. I am turning it into a bunch of tuples containing offset and length to make it more efficient to deal with in memory. However, this sometimes is less efficient with things like 10101010100011. Any ideas on a good w...

How to create efficient map system?

Hi, i have a map system (grid) for my website. I have defined 40000 'fields' on a grid. Each field has a XY value (for x(1-200) and y(1-200)) and a unique identifier: fieldid(1-40000). I have a viewable area of 16x9 fields. When the user visits website.com/fieldid/422 it displays 16x9 fields starting with fieldid 422 in the upperleft c...

MySQL performance – Multiple queries or one inefficient query?

Hi. I have three tables, each contain some common information, and some information that is unique to the table. For example: uid, date are universal among the tables, but one table can contain a column type while the other contains currency. I need to query the database and get the last 20 entries (date DESC) that have been entered in ...

What is the most efficient way to replace many string tokens in many files in Java?

What I want to accomplish is a tool that filters my files replacing the occurrences of strings in this format ${some.property} with a value got from a properties file (just like Maven's or Ant's file filtering feature). My first approach was to use Ant API (copy-task) or Maven Filtering component but both include many unnecessary depend...

Linq Efficiency question - foreach vs aggregates

Which is more efficient? //Option 1 foreach (var q in baseQuery) { m_TotalCashDeposit += q.deposit.Cash m_TotalCheckDeposit += q.deposit.Check m_TotalCashWithdrawal += q.withdraw.Cash m_TotalCheckWithdrawal += q.withdraw.Check } //Option 2 m_TotalCashDeposit = baseQuery.Sum(q => q.deposit.Cash); m_TotalCheckDeposit = baseQuery....

SQL Efficiency with Function

Hello, So I've got this database that helps organize information for academic conferences, but we need to know sometimes whether an item is "incomplete" - the rules behind what could make something incomplete are a bit complex, so I built them into a scalar function that just returns true if the item is complete and 0 otherwise. The pr...

Writing Efficient CSS

Sorry if this is waaayyy to basic of a question to be asked here. But here goes... Ok so in another question something was being discussed, and this link was mentioned: https://developer.mozilla.org/en/Writing_Efficient_CSS In that article, they say some things I didn't know, but before I ask about them, I should ask this... Does that...

What do i need to know about dynamic programming?

Started up solving UVa problems again as a way to pass time (going to the army in 6 weeks). I love writing Java, but end up using C / C++. It's not because IO is faster, no need to box data, more memory or use of unsigned, because its algorithm efficiency that counts. In short i am slowly constructing how to/article/code base for differ...

Should I use SQL cursors here?

I have recently read about how cursors should be avoided. Well, I would like to know if my usage of them is appropriate. I am creating a scripting engine that will work online(embedded in a page, server-side) This scripting engine will be used by "advanced" end users of this product. The product works very heavily with the database howe...

Average time spent on coding problems

What is a good time limit to aim for when working on problems from Top Coder, UVA, SPOJ, CodeJam, etc. I assume your coding experience correlates with your time spent so please also state the amount of coding experience for each time limit you provide. ...