micro-optimization

Should I use Java's String.format() if performance is important?

徘徊边缘 提交于 2019-11-26 11:58:48
We have to build Strings all the time for log output and so on. Over the JDK versions we have learned when to use StringBuffer (many appends, thread safe) and StringBuilder (many appends, non-thread-safe). What's the advice on using String.format() ? Is it efficient, or are we forced to stick with concatenation for one-liners where performance is important? e.g. ugly old style, String s = "What do you get if you multiply " + varSix + " by " + varNine + "?"; vs. tidy new style (String.format, which is possibly slower), String s = String.format("What do you get if you multiply %d by %d?", varSix

Performance / Space implications when ordering SQL Server columns?

房东的猫 提交于 2019-11-26 11:38:53
问题 Are there any considerations that should be taken into account when designing a new table with regards to the order in which columns should be declared? I tend to put the primary key first, followed by any foreign keys (usually surrogate key integers), followed by other columns, but a discussion with a colleague had us wondering whether SQL Server will pad our data, possibly to make it faster. Will SQL Server try and align our data on disk (with padding) to a specific byte alignment boundary

Which of these pieces of code is faster in Java?

余生长醉 提交于 2019-11-26 10:59:31
问题 a) for(int i = 100000; i > 0; i--) {} b) for(int i = 1; i < 100001; i++) {} The answer is there on this website (question 3). I just can\'t figure out why? From website: 3. a 回答1: When you get down to the lowest level (machine code but I'll use assembly since it maps one-to-one mostly), the difference between an empty loop decrementing to 0 and one incrementing to 50 (for example) is often along the lines of: ld a,50 ld a,0 loop: dec a loop: inc a jnz loop cmp a,50 jnz loop That's because the

Fastest way to strip all non-printable characters from a Java String

痞子三分冷 提交于 2019-11-26 10:19:46
问题 What is the fastest way to strip all non-printable characters from a String in Java? So far I\'ve tried and measured on 138-byte, 131-character String: String\'s replaceAll() - slowest method 517009 results / sec Precompile a Pattern, then use Matcher\'s replaceAll() 637836 results / sec Use StringBuffer, get codepoints using codepointAt() one-by-one and append to StringBuffer 711946 results / sec Use StringBuffer, get chars using charAt() one-by-one and append to StringBuffer 1052964 results

DateTime.DayOfWeek micro optimization

五迷三道 提交于 2019-11-26 09:45:07
问题 First of all: I\'m asking this question just for fun and eager to learn. I have to admit I love to mess around with micro-optimizations (Although they have never led to any significant increase in speed in any of my developments). The DateTime.DayOfWeek method does not represent a bottleneck in any application of mine. And it is highly unlikely to be a problem in any other. If anyone is thinking that this method has an impact on the performance of his application, he should think about When

&#39; … != null&#39; or &#39;null != …&#39; best performance?

送分小仙女□ 提交于 2019-11-26 09:34:38
问题 I wrote two methods to check there performance public class Test1 { private String value; public void notNull(){ if( value != null) { //do something } } public void nullNot(){ if( null != value) { //do something } } } and checked it\'s byte code after compiling public void notNull(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: getfield #2; //Field value:Ljava/lang/String; 4: ifnull 7 7: return LineNumberTable: line 6: 0 line 9: 7 StackMapTable: number_of_entries = 1 frame_type = 7 /*

Which is better option to use for dividing an integer number by 2?

老子叫甜甜 提交于 2019-11-26 08:58:09
问题 Which of the following techniques is the best option for dividing an integer by 2 and why? Technique 1: x = x >> 1; Technique 2: x = x / 2; Here x is an integer. 回答1: Use the operation that best describes what you are trying to do. If you are treating the number as a sequence of bits, use bitshift. If you are treating it as a numerical value, use division. Note that they are not exactly equivalent. They can give different results for negative integers. For example: -5 / 2 = -2 -5 >> 1 = -3

Branch alignment for loops involving micro-coded instructions on Intel SnB-family CPUs

梦想的初衷 提交于 2019-11-26 08:44:58
This is related, but not the same, as this question: Performance optimisations of x86-64 assembly - Alignment and branch prediction and is slightly related to my previous question: Unsigned 64-bit to double conversion: why this algorithm from g++ The following is a not real-world test case. This primality testing algorithm is not sensible. I suspect any real-world algorithm would never execute such a small inner-loop quite so many times ( num is a prime of size about 2**50). In C++11: using nt = unsigned long long; bool is_prime_float(nt num) { for (nt n=2; n<=sqrt(num); ++n) { if ( (num%n)==0

What is the fastest way to find if a number is even or odd?

不打扰是莪最后的温柔 提交于 2019-11-26 08:08:27
问题 What is the fastest way to find if a number is even or odd? 回答1: It is pretty well known that static inline int is_odd_A(int x) { return x & 1; } is more efficient than static inline int is_odd_B(int x) { return x % 2; } But with the optimizer on, will is_odd_B be no different from is_odd_A ? No — with gcc-4.2 -O2 , we get, (in ARM assembly): _is_odd_A: and r0, r0, #1 bx lr _is_odd_B: mov r3, r0, lsr #31 add r0, r0, r3 and r0, r0, #1 rsb r0, r3, r0 bx lr We see that is_odd_B takes 3 more

Is there a penalty when base+offset is in a different page than the base?

痴心易碎 提交于 2019-11-26 07:45:43
问题 The execution times for these three snippets: pageboundary: dq (pageboundary + 8) ... mov rdx, [rel pageboundary] .loop: mov rdx, [rdx - 8] sub ecx, 1 jnz .loop And this: pageboundary: dq (pageboundary - 8) ... mov rdx, [rel pageboundary] .loop: mov rdx, [rdx + 8] sub ecx, 1 jnz .loop And this: pageboundary: dq (pageboundary - 4096) ... mov rdx, [rel pageboundary] .loop: mov rdx, [rdx + 4096] sub ecx, 1 jnz .loop Are, on a 4770K, roughly 5 cycles per iteration for the first snippet and