Skip to content

Instantly share code, notes, and snippets.

@AdamStelmaszczyk
AdamStelmaszczyk / Bytecode
Last active August 29, 2015 14:23
Declaring/initializing variables inside or outside of a double (or multiple) loop
Compiled from "Test.java"
public class Test {
public Test();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public void testA();
Code:
@AdamStelmaszczyk
AdamStelmaszczyk / MyBenchmark.java
Created June 25, 2015 21:44
Should I use Java's String.format() if performance is important?
package org.sample;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.concurrent.TimeUnit;
@AdamStelmaszczyk
AdamStelmaszczyk / MyBenchmark.java
Last active December 19, 2015 16:20
Most efficient way to make the first character of a String lower case?
package org.sample;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.infra.Blackhole;
import java.lang.reflect.Field;
public class MyBenchmark {
@Benchmark
@AdamStelmaszczyk
AdamStelmaszczyk / MyBenchark.java
Last active August 16, 2021 05:26
Fastest way to check if comma separated string contains at least 1 valid integer?
package org.sample;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
@AdamStelmaszczyk
AdamStelmaszczyk / MyBenchmark.java
Last active December 19, 2015 16:11
Is returning a static empty array faster than creating a new one?
package org.sample;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.infra.Blackhole;
public class MyBenchmark {
private static final String[] EMPTY_STRING_ARRAY = new String[0];
@AdamStelmaszczyk
AdamStelmaszczyk / 10.prof
Last active June 8, 2017 16:42
How to optimize this Haskell code summing up the primes?
Thu Jun 8 18:42 2017 Time and Allocation Profiling Report (Final)
10 +RTS -p -RTS
total time = 7.79 secs (7786 ticks @ 1000 us, 1 processor)
total alloc = 6,259,859,408 bytes (excludes profiling overheads)
COST CENTRE MODULE %time %alloc
p.sieve.update.decrease Main 52.2 67.5
@AdamStelmaszczyk
AdamStelmaszczyk / 10.hs
Last active June 8, 2017 18:23
IntMap instead Map, Int instead Integer
import Data.List
import Data.IntMap (IntMap, (!))
import qualified Data.IntMap as IntMap
p :: Int -> Int
p n = (sieve (IntMap.fromList [(i, i * (i + 1) `div` 2 - 1) | i <- vs]) 2 r vs) ! n
where vs = [n `div` i | i <- [1..r]] ++ reverse [1..n `div` r - 1]
r = floor (sqrt (fromIntegral n))
@AdamStelmaszczyk
AdamStelmaszczyk / 10_v2.hs
Created June 9, 2017 12:42
After Shersh code review
import Data.List
import Data.IntMap.Strict(IntMap, (!))
import qualified Data.IntMap.Strict as IntMap
problem10 :: Int -> Int
problem10 n = (sieve (IntMap.fromList [(i, i * (i + 1) `div` 2 - 1) | i <- vs]) 2 r vs) ! n
where vs = [n `div` i | i <- [1..r]] ++ [n', n' - 1 .. 1]
r = floor (sqrt (fromIntegral n))
n' = n `div` r - 1
@AdamStelmaszczyk
AdamStelmaszczyk / slow.hs
Last active June 13, 2017 20:23
hashtables
import Data.List
import qualified Data.HashTable.IO as H
import Data.Maybe (fromJust)
type HashTable k v = H.BasicHashTable k v
(!) :: IO (HashTable Int Int) -> Int -> IO Int
(!) m k = do
h <- m
x <- H.lookup h k
/*
Compile:
g++ -O2 --std=c++11 10.cpp
Run:
time ./a.out
Results with g++ version 4.8.4:
sum(20000000) = 12272577818052 in 0.019s
sum(200000000) = 1075207199997334 in 0.063s