Skip to content

Instantly share code, notes, and snippets.

@zhangz
zhangz / ExceptionTest.cs
Created June 18, 2011 13:55
ExceptionTest
class ExceptionTest
{
long maxLevel = 20;
static void Main(string[] args)
{
ExceptionTest test = new ExceptionTest();
int count = 10000;
Stopwatch sw = new Stopwatch();
sw.Start();
/******************************************************************************
Module: Wintellect.cs
Notices: Copyright (c) 2008-2009 Jeffrey Richter
******************************************************************************/
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
public static class CodeTimer
{
public static void Initialize()
{
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
Thread.CurrentThread.Priority = ThreadPriority.Highest;
Time("", 1, () => { });
}
public static void Time(string name, int iteration, Action action)
/// <summary>
/// Determines the size of the object or struct created and returned by the
/// specified method. </summary>
/// <remarks>Should not be used in production! This is meant for use during
/// development, not as a general purpose sizeof function.</remarks>
/// <param name="maker">The method that creates and returns the object or
/// struct whose size will be determined.</param>
/// <returns>The size in bytes of the object created by the method.</returns>
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
public long ByteSize<T>(Func<T> maker)
class Program
{
private readonly Dictionary<Foo, int> dict = new Dictionary<Foo, int>();
private readonly List<Data> data = new List<Data>();
static void Main(string[] args)
{
Program p = new Program();
var foo = new Foo(1);
//foo.GetHashCode(); //this will cache hashcode in foo, but data.Foo is another object...
@zhangz
zhangz / cpu-usage-sin
Last active December 30, 2015 23:59
cpu正弦曲线
class Program
{
static void Main(string[] args)
{
Sin1 sin11 = new Sin1();
//4 core-cpu
Thread t1 = new Thread(sin11.DoWork);
Sin1ThreadParam p1 = new Sin1ThreadParam(0);
Thread t2 = new Thread(sin11.DoWork);
Sin1ThreadParam p2 = new Sin1ThreadParam(1);
@zhangz
zhangz / WeakEventHandler.cs
Created February 12, 2014 12:05
WeakEventHandler
public static class WeakEventHandler {
private class WeakEventHandlerImpl {
protected readonly WeakReference m_wrTarget; // WeakReference to original delegate's target object
protected Delegate m_openEventHandler; // "Open" delegate to invoke original target's delegate method
public WeakEventHandlerImpl(Delegate d) { m_wrTarget = new WeakReference(d.Target); }
// Match is used to compare a WeakEventHandlerImpl object with an actual delegate.
// Typically used to remove a WeakEventHandlerImpl from an event collection.
public Boolean Match(Delegate strongEventHandler) {
@zhangz
zhangz / vs shortcuts
Created March 5, 2014 03:37
vs shortcuts
Full-screen mode - Alt + Shift + Enter
Quick Lauch - Ctrl + Q
Search Solution Explorer - Ctrl + ;
Opens the menu on a class that isn't recognized and gives you import options - Alt + Shift + F10
Comment lines - Ctrl + K, Ctrl + C
Uncomment lines - Ctrl + K, Ctrl + U
#!/usr/bin/env ruby
# 1. export your RIL bookmarks
# 2. save this file to the same directory where your ril_export.html is
# 3. change username and password in the script bellow
# 4. run 'ruby ril_to_instapaper.rb' in terminal
require "cgi"
require "net/http"
require "net/https"
@zhangz
zhangz / NotNull
Created April 26, 2014 13:53 — forked from bleroy/NotNull
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
namespace Bleroy.Helpers {
public static class NotNull {
public static TProp Get<TSource, TProp>(this TSource source, Expression<Func<TSource, TProp>> property) where TSource : class {
if (source == null) return default(TProp);
var current = property.Body;