Skip to content

Instantly share code, notes, and snippets.

@fresky
fresky / Dataflow.cs
Last active August 29, 2015 13:59
Using Task.ContinueWith and TPL Dataflow to solve the pipeline problem
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
namespace TPLDataFlowExample
{
@fresky
fresky / CRTP.cpp
Created January 3, 2014 12:13
CRTP vs virtual function example.
#include<iostream>
using namespace std;
template<typename Derived> class Parent
{
public:
void SayHi()
{
@fresky
fresky / Counter_CRTP.cpp
Created January 3, 2014 05:20
Use CRTP to record the object counter
template <typename T>
struct counter
{
static int objects_created;
static int objects_alive;
counter()
{
++objects_created;
++objects_alive;
@fresky
fresky / Timer.cpp
Created December 24, 2013 08:28
Timer in CPP
#include"windows.h"
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
LARGE_INTEGER Frequency, PerformanceCount1, PerformanceCount2;
void TimeStart(){
QueryPerformanceCounter(&PerformanceCount1);
}
void TimeEnd(){
@fresky
fresky / gist_tag.rb
Created September 25, 2013 16:18
Fix the url issue in gist_tab.rb for octopress.
# A Liquid tag for Jekyll sites that allows embedding Gists and showing code for non-JavaScript enabled browsers and readers.
# by: Brandon Tilly
# Source URL: https://gist.github.com/1027674
# Post http://brandontilley.com/2011/01/31/gist-tag-for-jekyll.html
#
# Example usage: {% gist 1027674 gist_tag.rb %} //embeds a gist for this plugin
require 'cgi'
require 'digest/md5'
require 'net/https'
@fresky
fresky / ClassHierarchy.cpp
Created September 25, 2013 14:45
example to show the difference between dynamic cast and c style cast.
class BaseA
{
public:
virtual void foo()=0;
};
class BaseB
{
public:
virtual void bar(int a)=0;
@fresky
fresky / PerformanceWatcher.cs
Last active September 3, 2022 05:59
Time/Memory watcher for C#
public static double TimeWatcher(Action action)
{
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Start();
action();
watch.Stop();
var useTime = (double) watch.ElapsedMilliseconds/1000;
return useTime;
}
public static double TimeWatcher()
{
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Start();
// operations under test start
// ...
// operation under test stop
watch.Stop();
var useTime = (double) watch.ElapsedMilliseconds/1000;
return useTime;
@fresky
fresky / PythonStylePrint.cpp
Last active December 17, 2015 00:49
python style print in C++
namespace __hidden__ {
struct print {
bool space;
print() : space(false) {}
~print() { std::cout << std::endl; }
template <typename T>
print &operator , (const T &t) {
if (space) std::cout << ' ';
else space = true;
@fresky
fresky / BackupSQLServer.cs
Last active December 14, 2015 23:39
backup sql server
// add the following reference
//Microsoft.SqlServer.ConnectionInfo
//Microsoft.SqlServer.Management.Sdk.Sfc
//Microsoft.SqlServer.Smo
//Microsoft.SqlServer.SmoExtended
//Microsoft.SqlServer.SqlEnum
public void BackupDatabase(string databaseName, string userName, string password, string serverName,
string destinationPath)
{
//Define a Backup object variable.