Monday, February 13, 2012

std::bind vs lambda

Compared with std::bind, lambda is not just more elegant, but also has better performance.
struct A
{
 void test() const { std::cout << "test" << std::endl; }
 ~A() { std::cout << "~A" << std::endl; }
 int i;
};

struct Test
{
 void test(A m) const { m.test(); }
};

void test(A m) { m.test(); }

int main() 
{
  Test t;
  {
    A m;
    std::bind(&Test::test, &t, m)();
  }
  {
    A m;
    [=](){t.test(m);}();
  }
  {
    A m;
    std::bind(&test, m)();
  }
  {
    A m;
    [=](){test(m);}();
  }
  return 0;
}
# gcc 4.6.2
test
~A
~A
~A
~A
~A
test
~A
~A
~A
test
~A
~A
~A
test
~A
~A
~A
# gcc 4.5.1
~A
test
~A
~A
~A
~A
~A
~A
test
~A
~A
~A
~A
test
~A
~A
~A
~A
test
~A
~A
~A

Thursday, February 9, 2012

My First Android App

To get a free playbook from RIM, I spent my spare time to write an android app. One month ago, I got my playbook. I have long time not to buy any new electronic product, so touch and big screen give me a lot of fun. When I am at home, I am happy to read source code online with github while lying on bed. However, I have no 3G, so I wonder how to read source code when I am out of door though I stay at home most of time. When I got the news of RIM Offers Free PlayBook to Attract Android Developers
,  I made my mind to write an offline source code view app with android. I have no android experience before, so I need a simple solution to compose the app with shortest time. The final solution is to combine OI file manager and Syntax Hiligher. The first version is delivered to blackberry app world. This is also my first time to use Eclipse to write a real app, which changes my previous bad impression to Eclipse totally. Current Eclipse is really much more usable than before.
Apk
Bar



Friday, February 3, 2012

做事必须搞清10个顺序

  1. 职场:先升值,再升职;
  2. 沟通:先求同,再求异;
  3. 执行:先完成,再完美;
  4. 学习:先记录,再记忆;
  5. 设计:先仿造,再创造;
  6. 创业:先成长,再成功;
  7. 发展:先站住,再站高;
  8. 人际:先交流,再交心;
  9. 先做好自己,再要求别人;
  10. 先解决心情,再解决事情。

Sunday, January 15, 2012

A visual python debugger

pudb, it looks impressive from the picture below, though I seldom use any python debugger. pudb

New pub/sub system-Apache Kafka

Apache Kafka, it is from linkedin with a lot of specific characters. It aims at providing a unified stream for both real-time and offline consumption. It stores data in the file sequentially without random access. The status of consumer is stored in client side and the consumer works in pull way. It uses zero-copy method like sendfile (FileChannel.transferTo/transferFrom) to reduce copy cost. It really looks a good design. I think it's worth having a try. Here is a video about this. http://sna-projects.com/blog/2011/08/kafka/

Functional Thinking

A great video about functional programming Functional Thinking

The main idea is “Think about results, not steps”. It also covers more details about FP.

Thursday, January 12, 2012

How to model this pattern?

I am trying to improve the intra-day executions. For example, I wanna sell a stock but its mid price keeps monotone decreasing/increasing/unchanging at most of time (the small graph). occasionally, it has abrupt price fluctuation (the big graph). imbalance=ask depth - bid depth of top 5 price levels. Do you have any suggestion to model this to get good executions assuming time frame is 10 minutes? Are MACD hist and EMA applicable here? How to combine imbalance and price indicator?


Saturday, January 7, 2012

Substitute for sprintf

snprintf/sprintf are widely used in our code. In one program, it takes up >30% cpu usage from kcachegrind report. It is necessary to rewrite sprintf for performance concern. In stead of sprintf, I prefer C++ stream style. Such a simple implementation as follows is almost 3X faster.

typedef std::pair<const char*, size_t> StrN;
typedef std::pair<size_t, size_t> NN; 
template <size_t L>
class StrStream
{
public:
  StrStream() : p_(buf_) {}
  size_t length() const { return p_ - buf_; }
  const char* get() const { return buf_; }
  void reverse(char* a, char* b)
  {
    while (a < b) std::swap(*(a++), *(b--));
  }
  template <typename T> StrStream& operator<<(T n)
  {
    assert(n>=0);
    char* p = p_; 
    do {
      *(p_++) = n % 10 + '0';
    } while (n /= 10);
    reverse(p, p_-1);
    return *this;
  }
  StrStream& operator<<(char c)  
  {
    *(p_++) = c;
    return *this;
  }
  StrStream& operator<<(StrN p)
  {
    while (p.second-- > 0) { // no *str check
      *(p_++) = *(p.first++);
    }
    return *this;
  }
  StrStream& operator<<(const char* str)
  {
    while (*str) {
      *(p_++) = *(str++);
    }
    return *this;
  }
  StrStream& operator<<(char* str) { return operator<<((const char*)str); }
  StrStream& operator<<(NN n)
  {
    assert(n.first>=0);
    char* p = p_;
    do {
      *(p_++) = n.first % 10 + '0';
    } while (n.first /= 10);
    char* p2 = p + n.second;
    while (p_ < p2) *(p_++) = '0';
    reverse(p, p_-1);
    return *this;
  }

private:
  char buf_[L];
  char* p_;
};

int main() {
  clock_t t = clock();
  for (size_t i = 0; i < 10000000; ++i) {
    char buf[256];
    sprintf(buf, "%ld %04ld %04ld %.4s", i, i, i, "abcde");
  }
  std::cout << (clock() -t) << std::endl;
  t = clock();
  for (size_t i = 0; i < 10000000; ++i) {
    StrStream<256> s;
    s << i << ' ' << NN(i, 4) << ' ' << NN(i, 4) << ' ' << StrN("abcde", 4);
  }
  std::cout << (clock() -t) << std::endl;
  return 0;
}

13830000
3560000