This tag is associated with 40 posts

Why Is Printing “B” Dramatically Slower Than Printing “#”?

I generated two matrices of 1000 x 1000: First Matrix: O and #. Second Matrix: O and B. Using the following code, the first matrix took 8.52 seconds to complete: Random r = new Random(); for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000; j++) { … Continue reading »

Why Is Subtracting These Two Times (In 1927) Giving a Strange Result?

If I run the following program, which parses two date strings referencing times one second apart and compares them: public static void main(String[] args) throws ParseException { SimpleDateFormat sf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”); String str3 = “1927-12-31 23:54:07”; String str4 = “1927-12-31 23:54:08”; Date sDt3 = sf.parse(str3); Date sDt4 = sf.parse(str4); long ld3 = sDt3.getTime() … Continue reading »

Why Is Char[] Preferred Over String for Passwords?

In Swing, the password field has a getPassword() (returns char[]) method instead of the usual getText() (returns String) method. Similarly, I have come across a suggestion not to use Strings to handle passwords. Why does String pose a threat to security when it comes to passwords? It feels inconvenient to use char[]. Ahamed Answer: Strings … Continue reading »

With C arrays, why is it the case that a[5] == 5[a]?

As Joel points out in Stack Overflow podcast #34, in C Programming Language (aka: K & R), there is mention of this property of arrays in C: a[5] == 5[a] Joel says that it’s because of pointer arithmetic but I still don’t understand. Why does a[5] == 5[a] ? Dinah Answer: The C standard defines … Continue reading »

Bjarne Stroustrup: Why I Created C++

Why doesn’t GCC optimize a*a*a*a*a*a to (a*a*a)*(a*a*a)?

I am doing some numerical optimization on a scientific application. One thing I noticed is that GCC will optimize the call pow(a,2) by compiling it into a*a, but the call pow(a,6) is not optimized and will actually call the library function pow, which greatly slows down the performance. (In contrast, Intel C++ Compiler, executable icc, … Continue reading »

C++: Why is one loop so much slower than two loops?

Suppose a1, b1, c1, and d1 point to heap memory and my numerical code has the following core loop. const int n=100000 for(int j=0;j<n;j++){ a1[j] += b1[j]; c1[j] += d1[j]; } This loop is executed 10,000 times via another outer for loop. To speed it up, I changed the code to: for(int j=0;j<n;j++){ a1[j] += … Continue reading »

C++: Why does changing 0.1f to 0 slow down performance by 10x?

Why does this bit of code, const float x[16] = { 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6}; const float z[16] = {1.123, 1.234, 1.345, 156.467, 1.578, 1.689, 1.790, 1.812, 1.923, 2.034, 2.145, 2.256, 2.367, 2.478, 2.589, 2.690}; float y[16]; for (int i = 0; i … Continue reading »

Why is processing a sorted array faster than an unsorted array?

Here is a piece of C++ code that seems very peculiar. For some strange reason, sorting the data miraculously makes the code almost six times faster. #include <algorithm> #include <ctime> #include <iostream> int main() { // Generate data const unsigned arraySize = 32768; int data[arraySize]; for (unsigned c = 0; c < arraySize; ++c) data[c] … Continue reading »

Why Your Advanced Spam Filter Isn’t Enough