If I call getMeasuredWidth() or getWidth() for layout in onResume they return 0

You cannot use the width/height/getMeasuredWidth/getMeasuredHeight on a View before the system renders it (typically from onCreate/onResume). Simple solution for this is to post a Runnable to the layout. The runnable will be executed after the View has been laid out. BoxesLayout = (RelativeLayout) findViewById(R.id.BoxesLinearLayout); BoxesLayout.post(new Runnable() { @Override public void run() { int w = … Read more

Why is Graphics.MeasureString() returning a higher than expected number?

From WindowsClient.net: GDI+ adds a small amount (1/6 em) to each end of every string displayed. This 1/6 em allows for glyphs with overhanging ends (such as italic ‘f‘), and also gives GDI+ a small amount of leeway to help with grid fitting expansion. The default action of DrawString will work against you in displaying … Read more

rem px in Javascript

Another method would be to convert rem into pixels: function convertRemToPixels(rem) { return rem * parseFloat(getComputedStyle(document.documentElement).fontSize); } This can be useful when you must perform some arithmetic in js (such as using the position of the mouse to display a tooltip…)

Stopwatch class for Java

The Spring Framework has an excellent StopWatch class: StopWatch stopWatch = new StopWatch(“My Stop Watch”); stopWatch.start(“initializing”); Thread.sleep(2000); // simulated work stopWatch.stop(); stopWatch.start(“processing”); Thread.sleep(5000); // simulated work stopWatch.stop(); stopWatch.start(“finalizing”); Thread.sleep(3000); // simulated work stopWatch.stop(); System.out.println(stopWatch.prettyPrint()); This produces: StopWatch ‘My Stop Watch’: running time (millis) = 10000 —————————————– ms % Task name —————————————– 02000 020% initializing 05000 … Read more

simplest tool to measure C program cache hit/miss and cpu time in linux?

Use perf: perf stat ./yourapp See the kernel wiki perf tutorial for details. This uses the hardware performance counters of your CPU, so the overhead is very small. Example from the wiki: perf stat -B dd if=/dev/zero of=/dev/null count=1000000 Performance counter stats for ‘dd if=/dev/zero of=/dev/null count=1000000’: 5,099 cache-misses # 0.005 M/sec (scaled from 66.58%) … Read more