Skip to content

Instantly share code, notes, and snippets.

View zavakid's full-sized avatar
🍑
hungry

Zava zavakid

🍑
hungry
View GitHub Profile
@zavakid
zavakid / LinearizationLearn.scala
Last active January 7, 2017 01:34
scala linearizated
object LinearizationLearn {
case class Clazz(name: String, succ: List[Clazz])
val animal = Clazz("animal", Nil)
val furry = Clazz("furry", List(animal))
val hasLegs = Clazz("hasLegs", List(animal))
val fourLegged = Clazz("fourLegged", List(hasLegs))
val cat = Clazz("cat", List(animal, furry, fourLegged))
@zavakid
zavakid / busyThread.sh
Created September 20, 2014 11:47
dump the thread stack which the most wasted of CPU
#!/bin/bash
if [ $# -eq 0 ];then
echo "please enter java pid"
exit -1
fi
pid=$1
jstack_cmd=""
@zavakid
zavakid / LognameBasedDiscriminator.scala
Created January 22, 2014 04:32
logname base discriminator in scala.
package com.mogujie.dragon.exodus.core.commons
import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.core.sift.AbstractDiscriminator
import scala.beans.BeanProperty
/**
* author: luwu luwu@mogujie.com
* 2014 2014-01-22 上午11:57
*/

Latency numbers every programmer should know

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns
L2 cache reference ........................... 7 ns
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns             
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs

Read 1 MB sequentially from memory ..... 250,000 ns = 250 µs

@zavakid
zavakid / gist:5030465
Created February 25, 2013 15:13
exec a command with timeout
#/bin/bash
# 其实是使用了 perl 的 alarm
perl -e "alarm 10; exec @ARGV" "$@
@zavakid
zavakid / gist:4045648
Created November 9, 2012 13:24
liquid tokenize
text = "{{asd}} sdfadsf afas {% a %}xxx{% enda %} asdfa asdf s af "
text.split(Liquid::TemplateParser)
# result:
[
"",
"{{asd}}",
" sdfadsf afas ",
"{% a %}",
"xxx",
@zavakid
zavakid / SemaphoreTest.java
Created October 12, 2012 13:12
Semaphore hold the thread
package com.zavakid.concurrent;
import java.util.concurrent.Semaphore;
import org.junit.Test;
/**
* @author Zava 2012-10-12 下午1:06:15
* @version 1.0
*/
<?php
class StringRandomizer {
public function process( $text ) {
return preg_replace_callback('/\{(((?>[^\{\}]+)|(?R))*)\}/x', array( $this, 'replace' ), $text );
}
public function replace( $text ) {
$text = $this->process( $text[ 1 ] );
@zavakid
zavakid / gist:3045599
Created July 4, 2012 05:43
replace with regex and callback in PHP
<?php
function urlReplace($text, $replaceWith){
// set the callback function
$callback = function($match)use($replaceWith){
$replaced0 =preg_replace('/\{0\}/', $match[0], $replaceWith);
return preg_replace('/\{1\}/', toUpper($match[0]), $replaced0);
};
var_dump($callback);
return preg_replace_callback('/https?:\/\/\S+(?= |$)/',$callback, $text);
@zavakid
zavakid / gist:1857221
Created February 18, 2012 03:34
javaassist insert some code before method
package com.zavakid.javassist.test;
public class Hello {
public void say(){
System.out.println("Hello world.");
}
}
//======================
package com.zavakid.javassist.test;