Skip to content

Instantly share code, notes, and snippets.

View mhshams's full-sized avatar

Mohammad Sarbandi mhshams

  • Berlin, Germany
View GitHub Profile
@mhshams
mhshams / StringKeyTest.java
Created August 1, 2013 17:52
performance test, completed :)
public class StringKeyTest {
private static final SecureRandom SECURE_RANDOM = new SecureRandom();
private static final StringGenerator GENERATOR = () -> new BigInteger(130, SECURE_RANDOM).toString(32);
public static void main(String[] args) {
List<Key> keys = new ArrayList<>();
Map<String, String> withString = new HashMap<>();
@mhshams
mhshams / gist:6133552
Created August 1, 2013 17:40
the results
With Warm up String: 78933098
With Warm up Object: 24321005
With Warm up Builder: 71801287
With Warm up String: 63005060
With Warm up Object: 11406968
With Warm up Builder: 62253727
With Warm up String: 61489515
With Warm up Object: 12505735
With Warm up Builder: 60929296
With Warm up String: 62588585
@mhshams
mhshams / StringKeyTest.java
Created August 1, 2013 17:39
a performance test
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author me
*/
@mhshams
mhshams / ReflectionConstructorTest.java
Created January 17, 2013 16:55
Object instanciation with reflection API
import org.junit.Test;
import sun.reflect.ReflectionFactory;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
/**
* @author me
*/
class Node {
/*data to be save*/
def data
/*child nodes of this node*/
def children = [:]
/*any key finshied in this node? (end of a word)*/
def flag = false
}
class Trie {
class Node {
/*data to be save*/
def data
/*child nodes of this node*/
def children = [:]
}
class Trie {
/*root node with empty data*/
def root = new Node(data:'')
def introSort(list) {
// calculate the depth limit.
def depth = 2 * (int)(Math.log10(list.size()) / Math.log10(2))
return introLoop(list, depth)
}
def introLoop(list, depth) {
if (list.size() <= 1) {
return list
}
def log2First(v) {
def r = 0
while ((v >>= 1) != 0) r++
return r
}
def log2Second(v) {
def buffer = java.nio.ByteBuffer.allocate(8)
double d = buffer.putInt(0x43300000).putInt(v).getDouble(0) - 4503599627370496.0
return (buffer.putDouble(0, d).getInt(0) >> 20) - 0x3FF
class PriorityQueue {
/*a list to contains the queue elments in a binary heap format*/
def list = []
/*Add new item to the queue*/
def add(item) {
list.add(item)
/*After adding new item, we need to re-validate the heap*/
siftUp(list, 0, list.size() - 1)
def quickSort(list) {
/*Make sure there are at least 2 elements in the list*/
if (list == null || list.size() < 2) {
return list
}
/*break the list to three different groups
group 1: with elements less than pivot
group 2: with elements equal to pivot