efficiency

What puts less load on a PHP server: SimpleXML or json_decode?

I'm starting to develop a web application in PHP that I hope will become incredibly popular and make me famous and rich. :-) If that time comes, my decision whether to parse the API's data as XML with SimpleXML or to use json_decode could make a difference in the app's scalability. Does anyone know which of these approaches is more eff...

Whats the best method to maintain or measure how well sorted a collection is so we can choose best sort algorithm?

Inspired by this question The choice of which algorithm to use to sort a collection can be made better if we know ahead of time how well sorted a collection is. Is there a way we can measure (or maintain a measurement) of how well sorted the collection is? Can we do this in such a way that the cost of maintaining or measuring how well s...

Efficient heap-manager for heavy churn, tiny allocs?

I'm looking for ideas for a heap-manager to handle a very specific situation: Lots and lots of very small allocations, ranging from 12 to 64 bytes each. Anything bigger, I will pass on to the regular heap-manager, so only tiny blocks need be catered for. Only 4-byte alignment is needed. My main concerns are Overhead. The regular libc ...

Size of a byte in memory - Java

I have heard mixed opinions over the amount of memory that a byte takes up in a java program. I am aware you can store no more than +127 in a java byte, and the documentation says that a byte is only 8 bits but here I am told that it actually takes up the same amount of memory as an int, and therefore is just a Type that helps in code c...

Is there a memory efficient replacement of java.lang.String?

After reading this old article measuring the memory consumption of several object types, I was amazed to see how much memory Strings use in Java: length: 0, {class java.lang.String} size = 40 bytes length: 7, {class java.lang.String} size = 56 bytes While the article has some tips to minimize this, I did not find them entirely satisfy...

Is this prime generator inefficient C++?

Is this seen as an in efficient prime number generator. It seems to me that this is pretty efficient. Is it the use of the stream that makes the program run slower? I am trying to submit this to SPOJ and it tells me that my time limit exceeded... #include <iostream> #include <sstream> using namespace std; int main() { int testCa...

Finding numerical substrings mathematically, without string comparison.

This originally was a problem I ran into at work, but is now something I'm just trying to solve for my own curiosity. I want to find out if int 'a' contains the int 'b' in the most efficient way possible. I wrote some code, but it seems no matter what I write, parsing it into a string and then using indexOf is twice as fast as doing it...

How do I insert a linebreak where the cursor is without entering into insert mode in Vim?

Is possible to insert a linebreak where the cursor is in Vim without entering into insert mode? Here's an example ([x] means cursor is on x): if (some_condition) {[ ]return; } Occasionally, I might want to enter some more code. So I'd press 'i' to get into insert mode, press enter to insert the linebreak and then delete the extra spac...

Full-text search relevance is measured in?

I am making a quiz system, and when quizmakers insert questions into the Question Bank, I am to check the DB for duplicate / very highly similar questions. Testing MySQL's MATCH() ... AGAINST(), the highest relevance I get is 30+, when I test against a 100% similar string. So what exactly is the relevance? To quote the manual: Rele...

Relative performance of std::vector vs. std::list vs. std::slist?

For a simple linked list in which random access to list elements is not a requirement, are there any significant advantages (performance or otherwise) to using std::list instead of std::vector? If backwards traversal is required, would it be more efficient to use std::slist and reverse() the list prior to iterating over its elements? ...

Which is more efficient way to assign values to variables in .NET?

This is something that I have always wondered about, but never bothered to profile. Is it more efficient to assign a value to a temp variable, than to keep using that value. An Example may be clearer: string s = reader.GetItem[0].ToString(); someClass.SomeField = s; someOtherClass.someField = s; OR someClass.SomeField = reader.Get...

PHP: Right way to declare variable before use in loop

I have a variable that is built in loop. Something like: $str = ""; for($i = 0; $i < 10; $i++) $str .= "something"; If $str = "" is ommitted, I get undefined variable notice, but I thought php auto-declare a variable the first time it sees undeclared one? How do I do this right? ...

Case (or switch) in a for loop or for loop in a case (or switch)?

Can it be known in general whether or not placing a case within a for loop will result in bad assembly. I'm interested mainly in Delphi, but this is an interesting programming question, both in terms of style and performance. Here are my codez! case ResultList.CompareType of TextCompareType: begin LastGoodIndex := -1; ...

Palindrome detection efficiency

I got curious by Jon Limjap's interview mishap and started to look for efficient ways to do palindrome detection. I checked the palindrome golf answers and it seems to me that in the answers are two algorithms only, reversing the string and checking from tail and head. def palindrome_short(s): length = len(s) for i in xrange(0,lengt...

MySQL field management

When I make a query, I often wonder about this: Is it better to add extra field(s) to the table, or just get the values and calculate in your server side language? For example, table Student contains Student.score field. I want to get the grades. Do I get the score and make a bunch of if/else/switch for the grades? ...

Fastest way of finding object in collection with coordinates near point

I have collection of objects. Each object represents a coordinate range (ie, a block). What I want is to find the object near another coordinate in a given direction. Is there a way to do this without traversing the whole collection all the time? ...

Writing a more efficient clock function.

I have a clock feature in a VB.NET program that displays the time including the seconds. I currently have a timer constantly polling using NOW. I have to poll the system clock quite often because I need to have the second update in sync with the system clock. Is there a more direct to access the time only when the seconds change? Is th...

Is it more efficient to return a const reference

E.g. What's best out of these: std::string f() {} or const std::string& f() {} ...

Choosing when to instantiate classes

I recently wrote a class for an assignment in which I had to store names in an ArrayList (in java). I initialized the ArrayList as an instance variable private ArrayList<String> names. Later when I checked my work against the solution, I noticed that they had initialized their ArrayList in the run() method instead. I thought about this ...

Optimisation: use local files or databases for HTML

This follows on from this question where I was getting a few answers assuming I was using files for storing my HTML templates. Before recently I've always saved 'compiled' templates as html files in a directory (above the root). My templates usually have two types of variable - 'static' variables which are not replaced on every usage bu...