Rectangle 27 3

In a very general sense the term "Scripts" relates to code that is deployed and expected to run from the lexical representation. As soon as you compile the code and distribute the resulting output instead of the code it ceases to be a "Script".

Minification and obsfication of a script is not consided a compile and the result is still consider a script.

Are all scripts written in scripting languages? - Stack Overflow

scripting programming-languages
Rectangle 27 2

Analysis What is wrong with the code

int main(void)  
{  
    char i;   // Warning: unused variable
    foo();  
}
void foo(void)  
{  
    extern int i;  
    i = 130;  
    printf("%d", i);  
}

The variable i in main() is wholly unrelated to the i referred to in foo(). The variable i in main() is strictly local to main() and is not passed to foo(); foo() is unable to see that i.

On Mac OS X 10.7.5 with GCC 4.7.1, the files compile separately, but they can't be linked because there is no definition for variable i.

If we move the variable i outside of main(), then the program links:

extern void foo(void);
#include <stdio.h>
#include "file2.h"

char i = 'A';

int main(void)
{
    printf("i = %d (%c)\n", i, i);
    foo();
    printf("i = %d (%c)\n", i, i);
    return(0);
}
#include "file2.h"
#include <stdio.h>

void foo(void)
{
    extern int i;
    i = 130;
    printf("i = %d (%c)\n", i, i);
}

This now links and runs, but it invokes undefined behaviour. The linker does not notice that the size of i should be sizeof(int) but is sizeof(char). The assignment in foo(), though, goes trampling out of bounds of the i that is allocated. It doesn't cause visible damage in this program, but that's pure luck.

When I run it on a little-endian machine (Intel x86/64), the output is:

i = 65 (A)
i = 130 (?)
i = -126 (?)

When I ran it on a big-endian machine (SPARC), I got a different result (which isn't very surprising; the behaviour is undefined, so any result is valid):

i = 65 (A)
i = 130 (?)
i = 0 ()

The most significant byte of the 4-byte integer was written over the only byte of the 1-byte character, hence the zero in the third line out output. Note too that it was lucky that the character was allocated an address that was a multiple of 4 bytes; that was not guaranteed and a misaligned address for an int would have caused a bus error (though experimentation suggests that it doesn't happen on SPARC even when i is forced onto an odd address; I'm not sure what is happening).

The correct way to handle this is to avoid writing external variable declarations such as extern int i; in any source file; such declarations should only appear in a header that is used everywhere the variable needs to be used.

extern char i;
extern void foo(void);

With that change in place (and the corresponding extern int i; removed), then the output is self-consistent:

i = 65 (A)
i = -126 (?)
i = -126 (?)

Note the role of the header in keeping both file1.c and file2.c in line with each other. It ensures that the definition in file1.c matches the declaration in file2.h, and the use of file2.h in file2.c ensures that the declaration used there is correct, too. See also What are extern variables in C.

c - working of extern keyword - Stack Overflow

c gcc
Rectangle 27 3

There is no sense reasoning in the output of your code because as it stands your program exhibits Undefined Behavior.

Per paragraph 1.9/15 of the C++11 Standard:

The value computations of the operands of an operator are sequenced before the value computation of the result of the operator. If a side eect on a scalar object is unsequenced relative to either another side eect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undened.

Because there is no sequence point separating both mutations of i, Undefined Behavior ensues. Your compiler might not output anything, and the program might output differently on different compilers. But arguing about the output is unnecessary in this context.

If you separate the statements, the result will then come out as expected:

std::cout << i   << std::endl;  // 0
std::cout << i++ << std::endl;  // 0
std::cout << ++i << std::endl;  // 2

The second comment should say 0, not 1, no?

c++ - Postfix and prefix increment - Stack Overflow

c++
Rectangle 27 1

Compiler output: a decent starting point

tl;dr: See the end for code that compiles and should work.

To just solve the 0.0 problem, you could also special-case inputs of 0.0 with an FP compare of the source against 0.0. Use the compare result as a mask to zero out any NaNs resulting from 0 * +Infinity in sqrt(x) = x * rsqrt(x)). Clang does this when autovectorizing. (But it uses blendps with the zeroed vector, instead of using the compare mask with andnps directly to zero or preserve elements.)

It would also be possible to use sqrt(x) ~= recip(rsqrt(x)), as suggested by njuffa. rsqrt(0) = +Inf. recip(+Inf) = 0. However, using two approximations would compound the relative error, which is a problem.

Truncating to integer (instead of rounding) requires an accurate sqrt result when the input is a perfect square. If the result for 25*rsqrt(25) is 4.999999 or something (instead of 5.00001), you'll add 4 instead of 5.

Even with a Newton-Raphson iteration, rsqrtps isn't perfectly accurate the way sqrtps is, so it might still give 5.0 - 1ulp. (1ulp = one unit in the last place = lowest bit of the mantissa).

  • Newton Raphson SSE implementation performance (latency/throughput). Note that we care more about throughput than latency, since we're using it in a loop that doesn't do much else. sqrt isn't part of the loop-carried dep chain, so different iterations can have their sqrt calcs in flight at once.

It might be possible to kill 2 birds with one stone by adding a small constant before doing the (x+offset)*approx_rsqrt(x+offset) and then truncating to integer. Large enough to overcome the max relative error of 1.5*2-12, but small enough not to bump sqrt_approx(63*63-1+offset) up to 63 (the most sensitive case).

63*1.5*2^(-12)         ==  0.023071...
approx_sqrt(63*63-1)   == 62.99206... +/- 0.023068..

Actually, we're screwed without a Newton iteration even without adding anything

approx_sqrt(63*63-1)
n=36
sqrt(n*n-1) + error
sqrt(n*n)
define f(n) { local x=sqrt(n*n-1); local e=x*1.5*2^(-12); print x; print e, x+e; }
; f(36)
35.98610843089316319413
~0.01317850650545403926 ~35.99928693739861723339
; f(37)
36.9864840178138587015
~0.01354485498699237990 ~37.00002887280085108140

Does your source data have any properties that mean you don't have to worry about it being just below a large perfect square?

You could check all possible input values, since the important domain is very small (integer FP values from 0..63*63) to see if the error in practice is small enough on Intel Haswell, but that would be a brittle optimization that could make your code break on AMD CPUs, or even on future Intel CPUs. Unfortunately, just coding to the ISA spec's guarantee that the relative error is up to 1.5*2-12 requires more instructions. I don't see any tricks a NR iteration.

isqrt = static_cast<int> ((x+0.5)*approx_rsqrt(x+0.5))
20*20
20*20-1
; define test_approx_sqrt(x, off) { local s=x*x+off; local sq=s/sqrt(s); local sq_1=(s-1)/sqrt(s-1); local e=1.5*2^(-12); print sq, sq_1; print sq*e, sq_1*e; }
; test_approx_sqrt(20, 0.5)
~20.01249609618950056874 ~19.98749609130668473087   # (x+0.5)/sqrt(x+0.5)
 ~0.00732879495710064718  ~0.00731963968187500662   # relative error

Note that val * (x +/- err) = val*x +/- val*err. IEEE FP mul produces results that are correctly rounded to 0.5ulp, so this should work for FP relative errors.

The best bet is to add 0.5 to your input before doing an approx_sqrt using rsqrt. That sidesteps the 0/0 = NaN problem, and pushes the +/- error range all to one side of the whole number cut point (for numbers in the range we care about).

FP min/max instructions have the same performance as FP add, and will be on the critical path either way. Using an add instead of a max also solves the problem of results for perfect squares potentially being a few ulp below the correct result.

sum_int
-fno-math-errno -funsafe-math-optimizations
-ffinite-math-only
sqrt(0) = NaN
rsqrtps
sum_fp
-ffast-math

However clang's version suffers from the same problem as your idea: truncating an inexact result from rsqrt + NR, potentially giving the wrong integer. IDK if this is why gcc doesn't auto-vectorize, because it could have used sqrtps for a big speedup without changing the results. (At least, as long as all the floats are between 0 and INT_MAX2, otherwise converting back to integer will give the "indefinite" result of INT_MIN. (sign bit set, all other bits cleared). This is a case where -ffast-math breaks your program, unless you use -mrecip=none or something.

// autovectorizes with clang, but has rounding problems.
// Note the use of sqrtf, and that floorf before truncating to int is redundant. (removed because clang doesn't optimize away the roundps)
int sum_int(float vals[]){
  int sum = 0;
  for (int i = 0; i < MAX; i++) {
    int thisSqrt = (int) sqrtf(vals[i]);
    sum += std::min(thisSqrt, 0x3F);
  }
  return sum;
}

To manually vectorize with intrinsics, we can look at the asm output from -fno-unroll-loops (to keep things simple). I was going to include this in the answer, but then realized that it had problems.

I think converting to int inside the loop is better than using floorf and then addps. roundps is a 2-uop instruction (6c latency) on Haswell (1uop in SnB/IvB). Worse, both uops require port1, so they compete with FP add / mul. cvttps2dq is a 1-uop instruction for port1, with 3c latency, and then we can use integer min and add to clamp and accumulate, so port5 gets something to do. Using an integer vector accumulator also means the loop-carried dependency chain is 1 cycle, so we don't need to unroll or use multiple accumulators to keep multiple iterations in flight. Smaller code is always better for the big picture (uop cache, L1 I-cache, branch predictors).

As long as we aren't in danger of overflowing 32bit accumulators, this seems to be the best choice. (Without having benchmarked anything or even tested it).

I'm not using the sqrt(x) ~= approx_recip(approx_sqrt(x)) method, because I don't know how to do a Newton iteration to refine it (probably it would involve a division). And because the compounded error is larger.

#include <immintrin.h>
#define MAX 4096

// 2*sqrt(x) ~= 2*x*approx_rsqrt(x), with a Newton-Raphson iteration
// dividing by 2 is faster in the integer domain, so we don't do it
__m256 approx_2sqrt_ps256(__m256 x) {
    // clang / gcc usually use -3.0 and -0.5.  We could do the same by using fnmsub_ps (add 3 = subtract -3), so we can share constants
    __m256 three = _mm256_set1_ps(3.0f);
    //__m256 half = _mm256_set1_ps(0.5f);  // we omit the *0.5 step

    __m256 nr  = _mm256_rsqrt_ps( x );  // initial approximation for Newton-Raphson
    //           1/sqrt(x) ~=    nr  * (3 - x*nr * nr) * 0.5 = nr*(1.5 - x*0.5*nr*nr)
    // sqrt(x) = x/sqrt(x) ~= (x*nr) * (3 - x*nr * nr) * 0.5
    // 2*sqrt(x) ~= (x*nr) * (3 - x*nr * nr)
    __m256 xnr = _mm256_mul_ps( x, nr );

    __m256 three_minus_muls = _mm256_fnmadd_ps( xnr, nr, three );  // -(xnr*nr) + 3
    return _mm256_mul_ps( xnr, three_minus_muls );
}

// packed int32_t: correct results for inputs from 0 to well above 63*63
__m256i isqrt256_ps(__m256 x) {
    __m256 offset = _mm256_set1_ps(0.5f);    // or subtract -0.5, to maybe share constants with compiler-generated Newton iterations.
    __m256 xoff = _mm256_add_ps(x, offset);  // avoids 0*Inf = NaN, and rounding error before truncation
    __m256 approx_2sqrt_xoff = approx_2sqrt_ps256(xoff);
    __m256i i2sqrtx = _mm256_cvttps_epi32(approx_2sqrt_xoff);
    return _mm256_srli_epi32(i2sqrtx, 1);     // divide by 2 with truncation
    // alternatively, we could mask the low bit to zero and divide by two outside the loop, but that has no advantage unless port0 turns out to be the bottleneck
}

__m256i isqrt256_ps_simple_exact(__m256 x) {
    __m256 sqrt_x = _mm256_sqrt_ps(x);
    __m256i isqrtx = _mm256_cvttps_epi32(sqrt_x);
    return isqrtx;
}

int hsum_epi32_avx(__m256i x256){
    __m128i xhi = _mm256_extracti128_si256(x256, 1);
    __m128i xlo = _mm256_castsi256_si128(x256);
    __m128i x  = _mm_add_epi32(xlo, xhi);
    __m128i hl = _mm_shuffle_epi32(x, _MM_SHUFFLE(1, 0, 3, 2));
    hl  = _mm_add_epi32(hl, x);
    x   = _mm_shuffle_epi32(hl, _MM_SHUFFLE(2, 3, 0, 1));
    hl  = _mm_add_epi32(hl, x);
    return _mm_cvtsi128_si32(hl);
}

int sum_int_avx(float vals[]){
  __m256i sum = _mm256_setzero_si256();
  __m256i upperlimit = _mm256_set1_epi32(0x3F);

  for (int i = 0; i < MAX; i+=8) {
    __m256 v = _mm256_loadu_ps(vals+i);
    __m256i visqrt = isqrt256_ps(v);
    // assert visqrt == isqrt256_ps_simple_exact(v) or something
    visqrt = _mm256_min_epi32(visqrt, upperlimit);
    sum = _mm256_add_epi32(sum, visqrt);
  }
  return hsum_epi32_avx(sum);
}

Compiles on godbolt to nice code, but I haven't tested it. clang makes slightly nicer code that gcc: clang uses broadcast-loads from 4B locations for the set1 constants, instead of repeating them at compile time into 32B constants. gcc also has a bizarre movdqa to copy a register.

Anyway, the whole loop winds up being only 9 vector instructions, compared to 12 for the compiler-generated sum_int version. It probably didn't notice the x*initial_guess(x) common-subexpressions that occur in the Newton-Raphson iteration formula when you're multiplying the result by x, or something like that. It also does an extra mulps instead of a psrld because it does the *0.5 before converting to int. So that's where the extra two mulps instructions come from, and there's the cmpps/blendvps.

sum_int_avx(float*):
    vpxor   ymm3, ymm3, ymm3
    xor     eax, eax
    vbroadcastss    ymm0, dword ptr [rip + .LCPI4_0]  ; set1(0.5)
    vbroadcastss    ymm1, dword ptr [rip + .LCPI4_1]  ; set1(3.0)
    vpbroadcastd    ymm2, dword ptr [rip + .LCPI4_2]  ; set1(63)
LBB4_1:                                               ; latencies
    vaddps      ymm4, ymm0, ymmword ptr [rdi + 4*rax] ; 3c
    vrsqrtps    ymm5, ymm4                            ; 7c
    vmulps      ymm4, ymm4, ymm5   ; x*nr             ; 5c
    vfnmadd213ps ymm5, ymm4, ymm1                     ; 5c
    vmulps      ymm4, ymm4, ymm5                      ; 5c
    vcvttps2dq  ymm4, ymm4                            ; 3c
    vpsrld      ymm4, ymm4, 1      ; 1c this would be a mulps (but not on the critical path) if we did this in the FP domain
    vpminsd     ymm4, ymm4, ymm2                      ; 1c
    vpaddd      ymm3, ymm4, ymm3                      ; 1c
    ; ... (those 9 insns repeated: loop unrolling)
    add     rax, 16
    cmp     rax, 4096
    jl      .LBB4_1
    ;... horizontal sum

IACA thinks that with no unroll, Haswell can sustain a throughput of one iteration per 4.15 cycles, bottlenecking on ports 0 and 1. So potentially you could shave a cycle by accumulating sqrt(x)*2 (with truncation to even numbers using _mm256_and_si256), and only divide by two outside the loop.

Also according to IACA, the latency of a single iteration is 38 cycles on Haswell. I only get 31c, so probably it's including L1 load-use latency or something. Anyway, this means that to saturate the execution units, operations from 8 iterations have to be in flight at once. That's 8 * ~14 unfused-domain uops = 112 unfused-uops (or less with clang's unroll) that have to be in flight at once. Haswell's scheduler is actually only 60 entries, but the ROB is 192 entries. The early uops from early iterations will already have executed, so they only need to be tracked in the ROB, not also in the scheduler. Many of the slow uops are at the beginning of each iteration, though. Still, there's reason to hope that this will come close-ish to saturating ports 0 and 1. Unless data is hot in L1 cache, cache/memory bandwidth will probably be the bottleneck.

Interleaving operations from multiple dep chains would also be better. When clang unrolls, it puts all 9 instructions for one iteration ahead of all 9 instructions for another iteration. It uses a surprisingly small number of registers, so it would be possible to have instructions for 2 or 4 iterations mixed together. This is the sort of thing compilers are supposed to be good at, but which is cumbersome for humans. :/

It would also be slightly more efficient if the compiler chose a one-register addressing mode, so the load could micro-fuse with the vaddps. gcc does this.

c - Handling zeroes in _mm256_rsqrt_ps() - Stack Overflow

c x86 sse intrinsics avx
Rectangle 27 1

Here is the code for getting the current volume, the result output will be from 0.0 to 1.0.

Float32 volume;
UInt32 dataSize = sizeof(Float32);

Float32 currentVolume= AudioSessionGetProperty (
                         'chov',
                         &dataSize,
                         &volume
                         );

Thanks for the tip. But I must be doing something wrong. I always get the value 0.0 when I try this code. Should I be cautious about something when using it?

Finally it works. I was just looking at the wrong variable. I have to get the result in volume I expected it in currentVolume. Thanks again!

core audio - How to find the System Volume? - Stack Overflow

core-audio avaudioplayer audio
Rectangle 27 1

Then if you know what happens there, you can implement some code like ...

In fact, the keys are always sorted. If you output the map a couple of times, you will find that the result remains the same.

The reason is hashing. Each object has hashCode() method. The hash space is like a large array which contains all the possible hash values as indices. When a new element is inserted into a HashSet or a new pair is put into a HashMap, it is placed in the hash space according to its hash code. If two elements have the same hash code, they will be compared with equals() method, if unequal, then the new element will be placed next to it.

import java.util.*;

class MyString {
    private String str;

    public MyString (String str) {
        this.str = str;
    }

    public String toString () {
        return str;
    }

    public boolean equals (Object obj) {
        if (obj.getClass().equals(MyString.class)) {
            return obj.toString().equals(str);
        }
        return false;
    }

    public int hashCode () {
        if (str.equalsIgnoreCase("Not Categorized")) {
            return Integer.MAX_VALUE;
        } else if (str.hashCode() == Integer.MAX_VALUE) {
            return 0;
        }
        return str.hashCode();
    }
}

public class Test {
    public static void main (String args[]) {
        Map<MyString, String> m = new HashMap<MyString, String>();
        m.put(new MyString("a"), "a");
        m.put(new MyString("c"), "c");
        m.put(new MyString("Not Categorized"), "NC");
        m.put(new MyString("b"), "b");
        Set<MyString> keys = m.keySet();
        for (MyString k : keys) {
            System.out.println(m.get(k));
        }
    }
}

The result is "Not Categorized" always comes at last. The reason is simple: it's hash value is always the maximum of integer.

The reason I create a String wrapper class is String class is final, it can't be extended. So in this way, you would have your class structure a little change, but not much.

public static void main (String args[]) {
    Map<String, String> m = new TreeMap<String, String>(new Comparator<String>() {
        public int compare (String s1, String s2) {
            if (s1.equals(s2)) {
                return 0;
            }
            if (s1.equalsIgnoreCase("Not Categorized")) {
                return 1;
            }
            if (s2.equalsIgnoreCase("Not Categorized")) {
                return -1;
            }
            if (s1.hashCode() > s2.hashCode()) {
                return 1;
            } else if (s1.hashCode() < s2.hashCode()) {
                return -1
            } else {
                return 0;
            }
        }

        public boolean equals (Object obj) {
            return false;
        }
    });
    m.put("a", "a");
    m.put("c", "c");
    m.put("Not Categorized", "NC");
    m.put("b", "b");
    Set<String> keys = m.keySet();
    for (String k : keys) {
        System.out.println(m.get(k));
    }
}

java - Sorting of Map based on keys - Stack Overflow

java hashmap hashtable
Rectangle 27 9

The fact that what you see is the result of some JIT optimization should be clear by now looking at all the comments you received. But what is really happening and why that code is optimized always nearly after the same amount of iterations of the outer for?

I'll try to answer both questions but please remember that everything explained here is relative only to Oracle's Hotspot VM. There is no Java specification that defines how a JVM JIT should behave.

First of all, let's see what the JIT is doing running that Test program with some additional flag (the plain JVM is enough to run this, no need to load the debugging shared library, required for some of the UnlockDiagnosticVMOptions options):

java -XX:+PrintCompilation Test

The execution completes with this output (removing a few lines at the beginning that show that other methods are being compiled):

[...]
195017
184573
184342
184262
183491
189494
    131   51%      3       Test::getTimes @ 2 (22 bytes)
245167
    132   52       3       Test::getTimes (22 bytes)
165144  

65090
    132   53       1       java.nio.Buffer::limit (5 bytes)
59427
    132   54%      4       Test::getTimes @ 2 (22 bytes)  
75137
48110    
    135   51%     3        Test::getTimes @ -2 (22 bytes)   made not entrant

    142   55       4       Test::getTimes (22 bytes)
150820
86951
90012
91421

The printlns from your code are interleaved with diagnostic information related to the compilation the JIT is performing. Looking at a single line:

131    51%      3       Test::getTimes @ 2 (22 bytes)

Each column has the following meaning:

osr_bci
getTimes
131   51%      3       Test::getTimes @ 2 (22 bytes)
    132   52       3       Test::getTimes (22 bytes)
    132   54%      4       Test::getTimes @ 2 (22 bytes)     
    135   51%      3       Test::getTimes @ -2 (22 bytes)   made not entrant
    142   55       4       Test::getTimes (22 bytes)

It's clear that getTimes is being compiled more than once, but every time it's compiled in a different way.

That % symbol means that on-stack replacement(OSR) has been performed, meaning that the 10k loop contained in getTimes have been compiled isolated from the rest of the method and that the JVM replaced that section of the method code with the compiled version. The osr_bci is an index that points to this new compiled block of code.

The next compilation is a classic JIT compilation that compiles all the getTimes method (the size is still the same because there is nothing else in that method other than the loop).

The third time another OSR is performed but at a different tiered level. Tiered compilation have been added in Java7 and basically allows the JVM to choose the client or server JIT mode at runtime, switching freely between the two when necessary. The Client mode performs a simpler set of optimization strategies while the server mode is able to apply more sophisticated optimizations that on the other hand have a bigger cost in term of time spent compiling.

I will not go into details about the different modes or about tiered compilation, if you need additional information i recommend Java Performance: The Definitive Guide by Scott Oaks and also check this question that explain what changes between the levels.

Back to the output of PrintCompilation, the gist here is that from a certain point in time, a sequence of compilations with increasing complexity is performed until the method becomes apparently stable (i.e. the JIT doesn't compile it again).

So, why all this start at that certain point in time, after 5-10 iteration of the main loop?

Because the inner getTimes loop has become "hot".

The Hotspot VM, usually defines "hot" those methods that have been invoked at least 10k times (that's the historical default threshold, can be changed using -XX:CompileThreshold=<num>, with tiered compilation there are now multiple thresholds) but in the case of OSR i'm guessing that it's performed when a block of code is deemed "hot" enough, in term of absolute or relative execution time, inside the method contains it.

I second the recommendation, Java Performance: TPG is indeed invaluable whether you really need to squeeze out more performance from your application or you're just nosey.

optimization - Why is Java faster if it repeats the same code? - Stack...

java optimization jvm jit jvm-hotspot
Rectangle 27 4

You got 11.9999999999999982 !

If you add the following line to your code, you'll get an amazing result

print "%.16f" % n
11.9999999999999982

You may want to try this:

from decimal import Decimal

k=Decimal(12.0)
print int(k) #prints 12
12.0
Decimal
2.675
2.67499999999999982236431605997495353221893310546875
int
>>> int(11.9999999999)==11
True

I didn't downvote, but you didn't answer any of the question

int(12.0)
11

@ForceBru: No, you've shown that int(11.99999999) will give 11 not that 12.0 could be represented as 11.99999999

then it isn't an answer but a speculation. What part of an OS' representation of a number is used by python? -> This is the part where the bug is. And relates in this specific case most probably to the way python has been linked to a bugged library while being embedded into the application

Python integer unexpected results - Stack Overflow

python integer ironpython
Rectangle 27 6

Yes, json_encode escapes non-ascii characters. If you decode it you'll get your original result:

$string="";
echo "ENCODING: " . mb_detect_encoding($string) . "\n";
$encoded = json_encode($string);
echo "ENCODED JSON: $encoded\n";
$decoded = json_decode($encoded);
echo "DECODED JSON: $decoded\n";
ENCODING: UTF-8
ENCODED JSON: "\u3053\u3093\u306b\u3061\u306f"
DECODED JSON:

The self-documenting format that describes structure and field names as well as specific values;

It uses Unicode NOT UTF-8. This FAQ Explains the difference between UTF-8 and Unicode:

php - Problem json_encode utf-8 - Stack Overflow

php utf-8 character-encoding json
Rectangle 27 78

The result of your code will be the same. The reason is that the two incrementation operations can be seen as two distinct function calls. Both functions cause an incrementation of the variable, and only their return values are different. In this case, the return value is just thrown away, which means that there's no distinguishable difference in the output.

However, under the hood there's a difference: The post-incrementation i++ needs to create a temporary variable to store the original value of i, then performs the incrementation and returns the temporary variable. The pre-incrementation ++i doesn't create a temporary variable. Sure, any decent optimization setting should be able to optimize this away when the object is something simple like an int, but remember that the ++-operators are overloaded in more complicated classes like iterators. Since the two overloaded methods might have different operations (one might want to output "Hey, I'm pre-incremented!" to stdout for example) the compiler can't tell whether the methods are equivalent when the return value isn't used (basically because such a compiler would solve the unsolvable halting problem), it needs to use the more expensive post-incrementation version if you write myiterator++.

  • You won't have to think about whether the variable/object might have an overloaded post-incrementation method (for example in a template function) and treat it differently (or forget to treat it differently).
  • When someone asks you "Why do you pre-increment?" you'll get the chance to teach them about the halting problem and theoretical limits of compiler optimization. :)

I think that you are wrong! The only difference between ++i and i++ is the order that you do things. In one case you would inc *i push *i and in the other you would push *i inc *i I would not concider this an optimization.

He's not wrong. Post-incrementing primitives when you don't use the copy can get optimized out quite easily by compilers. But the point he's alluding to in the last bit is that post-incrementing on iterators is implemented as a method call and the construction of an entirely new copy of the iterator. These can't be optimized out by the compiler because constructors and function calls can have side effects the compiler can't track and therefore can't assume aren't critical.

> In one case you would inc *i push *i and in the other you would push *i inc *i I would not concider this an optimization.

He is wrong: I tried pre and post increment on compiler explorer here: godbolt.org and you get exactly the same assembly whichever way you put the increment. With C you're just describing what you want to happen to the compiler, if it amounts to the same thing then you usually get the same output, compilers are very clever these days. Pre and post increment almost never makes any difference - if you don't believe me try compiler explorer!

c++ - Post-increment and pre-increment within a 'for' loop produce sam...

c++ c order-of-evaluation
Rectangle 27 6

I am not sure whether exactly what code you have in the servlet. But I have created a sample Servlet and it returned the Json output with the same above code.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("application/json; charset=UTF-8");
        PrintWriter printout = response.getWriter();

        JSONObject JObject = new JSONObject(); 
        JObject.put("Response", "1"); 
        JObject.put("Message", "Client unauthorized"); 

        printout.print(JObject);
        printout.flush();
            // Or
            // printout.write(JObject.toString()); 
    }
{"Message":"Client unauthorized","Response":"1"}

Here is the result snap shot:

java - JSON response is returning as text - Stack Overflow

java json servlets httpresponse
Rectangle 27 6

I am not sure whether exactly what code you have in the servlet. But I have created a sample Servlet and it returned the Json output with the same above code.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("application/json; charset=UTF-8");
        PrintWriter printout = response.getWriter();

        JSONObject JObject = new JSONObject(); 
        JObject.put("Response", "1"); 
        JObject.put("Message", "Client unauthorized"); 

        printout.print(JObject);
        printout.flush();
            // Or
            // printout.write(JObject.toString()); 
    }
{"Message":"Client unauthorized","Response":"1"}

Here is the result snap shot:

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

java - JSON response is returning as text - Stack Overflow

java json servlets httpresponse
Rectangle 27 25

Ah, I think what is happening here is that your code is both correct, and working exactly as it should, and what you're interpreting here is a result of an overlap of two entirely understandable assumptions, that are actually not correct and have been leading you astray. So the good news is, your code is already working.

...which shows in the output that the function is being called...

If by that you mean that in the console you are seeing the message, openParentApplication called in button function, then here's what is going on:

This part of your code is a Swift Closure:

{(reply, error) -> Void in
        println("openParentApplication called in button function")
}

When your WatchKit Extension calls WKInterfaceController.openParentApplication it passes to your parent iPhone app a dictionary (your testDict), which the iPhone application can use to return data to youproviding the data has been serialized. It also returns back to you the closure that you passed it. This enables your WatchKit Extension to run code that it has itself defined, at the later point when the reply has been received. You can include in this closure use of both the returned data in the testDict and also other variables that were locally accessible at the time openParentApplication was called. Your WatchKit Extension automatically executes the code in the closure when it is received back.

So when you see openParentApplication called in button function, this indicates that the reply from the iPhone application has been received, and the closure has been executed. Therefore, your WatchKit test code should really change the println statement to be:

WKInterfaceController.openParentApplication(testDict,
    reply: {(reply, error) -> Void in
        println("Reply to openParentApplication received from iPhone app")
    })

Now, the reason why you quite understandably didn't realise the code was executing correctly was because you were expecting to see rejected in the console that this code had been executed in your iPhone app:

println("we made it!")

However, Xcode does not support attaching to two processes simultaneously. Therefore, when you are attached to your WatchKit app, you will not see any log messages of your iPhone app. Your iPhone app also will not respond to breakpoints when it is not the attached process. Both of these are true whether it is running in the background (woken by openParentApplication) or running in the foreground (if you launch it manually in the simulator after the WatchKit app has been run. You can see the effects of the iPhone app activity, but cannot directly introspect it while you are attached to the WatchKit app.

So firstly, your code is working correctly. You can move past your test code! And in relation to introspecting the workings in the iPhone side when it is responding to your WatchKit app, there is a partial solution. Launch the WatchKit app from the simulator, and once it is running, in Xcode activate the menu option Debug > Attach to process... and select your iPhone app process under Likely targets at the top. Now you will see your iPhone app console messages and your iPhone app will respond to breakpointsbut of course you will no longer see these from the WatchKit app side. You continue to be able to interact with both apps in the simulators, and can swap back and forth between which one you are attached to during execution.

Thank you so much for this! selecting my iPhone app under likely targets still doesn't seem to be showing me the output from the iPhone app (in fact it doesn't show any output at all) but I made a partial solution for a partial solution by passing '.description' back as an item in the reply for each thing I wanted to show and printed them in the Watchkit Extension output. but everything is now working perfectly - thanks for the help!

For a code example on how to use the background mode, which ensures that the main app has time to send its reply, refer to "Calling parent application from Watch app" (stackoverflow.com/questions/30000274/)

One important point: Xcode can attach to multiple processes at the same time for debugging this: After starting the Watch Kit App, in the iPhone Simulator click on your main app's icon (to start it) and then go back to Xcode. In the menu bar in the 'debug' menu, select 'attach to process' and select your main app. Now you are debugging both, your Watch Kit Extension and your main app!

Ups, just saw the second answer.. nvm.

ios - handleWatchKitExtensionRequest not responding to openParentAppli...

ios swift watchkit watch-os
Rectangle 27 4

You probably can't get exactly the json format you wanted, but you can get the same information by extending the callback classes and write your own handlers to events that you are interested.

Also make sure that you give the classes to the Playbook instance like this:

pb = ansible.playbook.PlayBook(
    ... # basic info
    callbacks= playbook_cb,
    runner_callbacks=runner_cb
)

Edit: In this case the solution was to extend the AggregateStat class instead. Code examples below provided by @Tom_Sawyyer

from ansible.callbacks import AggregateStats                                 

class CustomAggregateStats(AggregateStats):                                         
"""                                                                             
Holds stats about per-host activity during playbook runs.                       
"""                                                                             
def __init__(self):                                                             
    super(CustomAggregateStats, self).__init__()                                
    self.results = {}                                                           

def compute(self, runner_results, setup=False, poll=False,                      
            ignore_errors=False):                                               
    """                                                                         
    Walk through all results and increment stats.                               
    """                                                                         
    super(CustomAggregateStats, self).compute(runner_results, setup, poll,   
                                              ignore_errors)                    

    for (host, value) in runner_results.get('contacted', {}).iteritems():       
        if 'invocation' in value:                                               
            if value['invocation']['module_name'] == 'service':            
                self.results['service_name'] = value['name']                        

def summarize(self, host):                                                      
    """                                                                         
    Return information about a particular host                                  
    """                                                                         
    summarized_info = super(CustomAggregateStats, self).summarize(host)         

    # Adding the info I need                                                    
    summarized_info['result'] = self.results                                    

    return summarized_info
my_stats = CustomAggregateStats()
pb = ansible.playbook.PlayBook(
    ... # basic info
    stats=my_stats,
)
{"127.0.0.1": {"skipped": 0, "ok": 3, "changed": 2, "result": {"service_name": "apache2"}, "failures": 0, "unreachable": 0}}

Using the Ansible Python API, how can I get access to task level outpu...

python python-2.7 ansible ansible-playbook
Rectangle 27 4

Since this is a one-off job, I'd make do with a script (choose your favorite language; I'd probably pick Perl) and ImageMagick. You could use C# to accomplish the same as the script, although with more code. Just call the command line utilities and parse the resulting output.

The script to check a pair for similarity would be about 10 lines as follows:

First retrieve the sizes with identify and check aspect ratios nearly the same. If not, no match. If so, then scale the larger image to the size of the smaller with convert. You should experiment a bit in advance with filter options to find the one that produces the most similarity in known-equivalent images. Nine of them are available.

Then use the compare function to produce a similarity metric. Compare is smart enough to deal with translation and cropping. Experiment to find a similarity threshold that doesn't provide too many false positives.

c# - Finding matches between high quality and low quality, pixelated i...

c# image algorithm image-processing pattern-matching
Rectangle 27 1

Please save the below code in anyname.bat format and run it. It will output the results in a separate text file.

:: This Windows Batch(CMD) File fetches All the Details of the Nearby PC's of Same VLAN (Upto 254 host's).
    :: Windows OS (CMD)
    :: Author : [M.S.Arun][1]

    :: #****************************************************************** Start of Script ********************************************************************#

    @echo off
    title Remote PC Details Fetching Script(PC Name / IP's / Computer Description)
    echo. > %cd%\PC_Details_Temp.txt
    echo Remote PC Details Fetching Script (PC Name / IP's / Computer Description) details of the Nearby PC's of Same VLAN.(Upto 254 Hosts)
    echo.
    set /p input_ip="Please Enter the IP Range(Eg:192.168.1) :  " && echo
    set /p input_ip_start="Please Enter Start IP Range(Eg:1) :  " && echo
    set /p input_ip_end="Please Enter End IP Range(Eg:254) :  " && echo
    echo. >> %cd%\PC_Details_Temp.txt
    @echo on
    for /l %%i in (%input_ip_start%, 1, %input_ip_end%) do nbtstat -a %input_ip%.%%i | findstr /c:"MAC" /c:"<00>" | findstr /c:"MAC" /c:"UNIQUE" >> %cd%\PC_Details_Temp.txt && echo     IP Address  = %input_ip%.%%i >> %cd%\PC_Details_Temp.txt
    @echo off
    echo. > %cd%\PC_Details_Logs.txt
    echo. > %cd%\PC_Details_Logs.txt
    echo This Batch Script fetches All the Details of the Nearby PC's of Same VLAN.(Starting from 1 to 254 host's) >> %cd%\PC_Details_Logs.txt
    echo. >> %cd%\PC_Details_Logs.txt
    echo. >> %cd%\PC_Details_Logs.txt
    echo PC Host Name: >> %cd%\PC_Details_Logs.txt
    find "UNIQUE" PC_Details_Temp.txt >> %cd%\PC_Details_Logs.txt
    echo. >> %cd%\PC_Details_Logs.txt
    echo PC IP Address: >> %cd%\PC_Details_Logs.txt
    find "IP" PC_Details_Temp.txt >> %cd%\PC_Details_Logs.txt
    echo. >> %cd%\PC_Details_Logs.txt
    echo PC MAC Address: >> %cd%\PC_Details_Logs.txt
    find "MAC" PC_Details_Temp.txt >> %cd%\PC_Details_Logs.txt
    echo. >> %cd%\PC_Details_Logs.txt
    echo PC Seat No's. and Vnet No's: >> %cd%\PC_Details_Logs.txt
    echo. >> %cd%\PC_Details_Logs.txt
    echo. >> %cd%\PC_Details_Logs.txt
    net view /all >> %cd%\PC_Details_Logs.txt
    echo. >> %cd%\PC_Details_Logs.txt
    echo. >> %cd%\PC_Details_Logs.txt
    arp -a >> %cd%\PC_Details_Logs.txt
    :: del %cd%\PC_Details_Temp.txt
    echo.
    echo Completed Successfully..!
    echo.
    pause

    :: #****************************************************************** End of Script ********************************************************************#

windows - Get IP from MAC address. arp -a not showing device - Stack O...

windows batch-file networking command-line arp
Rectangle 27 8

I use a static method and swith statement to return exactly what I want. In the database I store tinyint and my code only uses the actual enum, so the strings are for UI requirements. After numerous testing this resulted in the best performance and most control over the output.

public static string ToSimpleString(this enum)
{
     switch (enum)
     {
         case ComplexForms:
             return "ComplexForms";
             break;
     }
}

public static string ToFormattedString(this enum)
{
     switch (enum)
     {
         case ComplexForms:
             return "Complex Forms";
             break;
     }
}

However, by some accounts, this leads to a possible maintenance nightmare and some code smell. I try to keep an eye for enums that are long and a lot of enums, or those that change frequently. Otherwise, this has been a great solution for me.

That's kinda what I was suggesting ;-)

C# String enums - Stack Overflow

c# string enums enumerator
Rectangle 27 8

I use a static method and swith statement to return exactly what I want. In the database I store tinyint and my code only uses the actual enum, so the strings are for UI requirements. After numerous testing this resulted in the best performance and most control over the output.

public static string ToSimpleString(this enum)
{
     switch (enum)
     {
         case ComplexForms:
             return "ComplexForms";
             break;
     }
}

public static string ToFormattedString(this enum)
{
     switch (enum)
     {
         case ComplexForms:
             return "Complex Forms";
             break;
     }
}

However, by some accounts, this leads to a possible maintenance nightmare and some code smell. I try to keep an eye for enums that are long and a lot of enums, or those that change frequently. Otherwise, this has been a great solution for me.

C# String enums - Stack Overflow

c# string enums enumerator
Rectangle 27 15

For your panel or a page from within your extension loaded in a tab

In Firefox, your code works (outputs to console), as it is written in the question.

If you are not seeing it in the console, then you are, probably, looking at the wrong console.

Mozilla describes what extension output can be seen in which console in their Debugging page.

You should be using the Browser Console. You can access it from ToolsWeb DeveloperBrowser Console (keyboard shortcut Ctrl-Shift-J, or Cmd-Shift-J on Mac).

If you have it enabled, you could also use the Browser Toolbox console. You can access it from ToolsWeb DeveloperBrowser Toolbox (keyboard shortcut Ctrl-Alt-Shift-I; On a Mac: Cmd-Alt-Shift-I).

To debug your add-on you can use the Add-on Debugger. You can access it though about:debuggingDebug.

You, probably, are looking at the Web Console (keyboard shortcut F12) which is associated with only a single tab. This is what you want when debugging a webpage, but not an add-on. For a content script which is injected in that tab, the console.log() output will show up in this console. However, you will not see output from any other portion of your add-on (e.g. not content scripts in other tabs, not background scripts, etc.).

Showing the correct console for your extension is a bit more complex in Chrome. Console output will show up in only one of multiple possible places, depending on from what context the console.log() was executed. Each of the following DevTools are independent of each other and are displayed in separate windows, or tabs. Displaying in the associated tab (bottom or side) is the default for the DevTools associated with web pages and content scripts, because those are specific to the tab. For the web page/content script DevTools, you have the option of having it displayed in its own separate window, or docked inside the tab (side or bottom).

As Srujan Reddy explained, you have to go through multiple selections on a drop-down menu, to get to the chrome://extensions page (or you can type that in by hand as the URL, or use a bookmark) then select both a checkbox ("Developer mode") and then click on the "background page" link. Then, you have to select the "Console" tab on the window that pops up.

Output will be shown in the regular web console (in the web Developer Tools). You can open it by pressing F12 (or other shortcuts) in the webpage in which your content script was injected. Each web console will only show the output from the scripts injected in that tab.

Doing the above with show the console.* output from your extension, but will result in the console JavaScript command line, debugger, etc. being in the context of the page, not the content script.

If you want to use the console JavaScript command line in the context of the content scripts which are injected into a web page, you need to select your extension's content script context from the drop-down menu in the upper left of the Console window. This drop-down menu will normally start with the value "top". The drop down will have selections for each of the content script contexts (one per extension that has script(s) injected).

Right-click on your browserAction button and select "Inspect Popup". Alternately, right-click within the popup and select "Inspect". Either will open the DevTools for the popup page. The popup will be kept open under more conditions than it normally would, but will still be closed if you switch tabs, etc.

Right-click within the main content of the Options popup (not the title bar) and select "Inspect". This will open the DevTools for the options page.

When the panel or tab is focused, you can open the DevTools by pressing F12 (or other shortcuts), or by opening the context menu (right-click) and selecting "Inspect".

@gal007, What are you not seeing? In which console? I'm seeing popup console.log() output just fine in Firefox Developer Edition v55b3 in the Browser Console, the Browser Toolbox and the Add-on Debugger. In Firefox Nightly v56, I also see popup console.log() output in all three tools. I did experience problems with the Browser Toolbox and Add-on Debugger when I tried to use them simultaneously in multiple instances of Firefox. However, they were broken completely (in all but the first instance started, when trying to use >1 instance of Firefox using those tools), not just for popup output.

Google Chrome / Firefox do not see extension output in console - Stack...

google-chrome-extension firefox-addon google-chrome-devtools firefox-developer-tools firefox-webextensions
Rectangle 27 15

For your panel or a page from within your extension loaded in a tab

In Firefox, your code works (outputs to console), as it is written in the question.

If you are not seeing it in the console, then you are, probably, looking at the wrong console.

Mozilla describes what extension output can be seen in which console in their Debugging page.

You should be using the Browser Console. You can access it from ToolsWeb DeveloperBrowser Console (keyboard shortcut Ctrl-Shift-J, or Cmd-Shift-J on Mac).

If you have it enabled, you could also use the Browser Toolbox console. You can access it from ToolsWeb DeveloperBrowser Toolbox (keyboard shortcut Ctrl-Alt-Shift-I; On a Mac: Cmd-Alt-Shift-I).

To debug your add-on you can use the Add-on Debugger. You can access it though about:debuggingDebug.

You, probably, are looking at the Web Console (keyboard shortcut F12) which is associated with only a single tab. This is what you want when debugging a webpage, but not an add-on. For a content script which is injected in that tab, the console.log() output will show up in this console. However, you will not see output from any other portion of your add-on (e.g. not content scripts in other tabs, not background scripts, etc.).

Showing the correct console for your extension is a bit more complex in Chrome. Console output will show up in only one of multiple possible places, depending on from what context the console.log() was executed. Each of the following DevTools are independent of each other and are displayed in separate windows, or tabs. Displaying in the associated tab (bottom or side) is the default for the DevTools associated with web pages and content scripts, because those are specific to the tab. For the web page/content script DevTools, you have the option of having it displayed in its own separate window, or docked inside the tab (side or bottom).

As Srujan Reddy explained, you have to go through multiple selections on a drop-down menu, to get to the chrome://extensions page (or you can type that in by hand as the URL, or use a bookmark) then select both a checkbox ("Developer mode") and then click on the "background page" link. Then, you have to select the "Console" tab on the window that pops up.

Output will be shown in the regular web console (in the web Developer Tools). You can open it by pressing F12 (or other shortcuts) in the webpage in which your content script was injected. Each web console will only show the output from the scripts injected in that tab.

Doing the above with show the console.* output from your extension, but will result in the console JavaScript command line, debugger, etc. being in the context of the page, not the content script.

If you want to use the console JavaScript command line in the context of the content scripts which are injected into a web page, you need to select your extension's content script context from the drop-down menu in the upper left of the Console window. This drop-down menu will normally start with the value "top". The drop down will have selections for each of the content script contexts (one per extension that has script(s) injected).

Right-click on your browserAction button and select "Inspect Popup". Alternately, right-click within the popup and select "Inspect". Either will open the DevTools for the popup page. The popup will be kept open under more conditions than it normally would, but will still be closed if you switch tabs, etc.

Right-click within the main content of the Options popup (not the title bar) and select "Inspect". This will open the DevTools for the options page.

When the panel or tab is focused, you can open the DevTools by pressing F12 (or other shortcuts), or by opening the context menu (right-click) and selecting "Inspect".

@gal007, What are you not seeing? In which console? I'm seeing popup console.log() output just fine in Firefox Developer Edition v55b3 in the Browser Console, the Browser Toolbox and the Add-on Debugger. In Firefox Nightly v56, I also see popup console.log() output in all three tools. I did experience problems with the Browser Toolbox and Add-on Debugger when I tried to use them simultaneously in multiple instances of Firefox. However, they were broken completely (in all but the first instance started, when trying to use >1 instance of Firefox using those tools), not just for popup output.

Google Chrome / Firefox do not see extension output in console - Stack...

google-chrome-extension firefox-addon google-chrome-devtools firefox-developer-tools firefox-webextensions