JMK no matter what

/quickpost/201005071256

C++ 인라이닝의 힘

In [252]: time tree = regtree.learn_tree(input[:20000], output[:20000],
 minsize=5, minsplit=1)
2010-05-07 12:55:50,972 INFO     learn_tree: N=20000, p=21,
 minsize=5 => 11641 nodes
CPU times: user 39.24 s, sys: 0.02 s, total: 39.26 s
Wall time: 39.26 s

In [254]: time tree2 = regtree.learn_tree_inlined(input[:20000], output[:20000], 
minsize=5, minsplit=1)
2010-05-07 12:56:02,128 INFO     learn_tree_inlined: N=20000, p=21, 
minsize=5 => 11641 nodes
CPU times: user 0.46 s, sys: 0.00 s, total: 0.46 s
Wall time: 0.45 s

In [256]: 39.26/0.45
Out[256]: 87.24444444444444
2010-05-08 02:58:00 | JM | /quickpost/ | 9 개의 댓글들
m.
2010-05-08 19:43:34
마치 옵티마이즈 끄고 컴파일한거랑 옵티마이즈 켜고 컴파일 한거 차이 같네.
JM
2010-05-13 00:32:41
@m, 더 간단한 코드에서는 100배 차이는 우습게 볼 수 있다능..
lqez
2010-05-24 01:20:37
아, 이거 완전 뒷북인데, 요새 컴파일러들이 인라인 잘 안해줌? C++은 직접 작성해서 빌드해본게 넘 오래되서 모르겠네.
dg
2010-05-25 11:11:06
흠... 100배 차이라니.. 한번 코드를 보고 싶군.. 함수 호출 비용 아낀다고 100배가 나올거 같지는 않고 인라인을 하면서 함수 경계 넘어서까지 최적화가 되는게 아닌가 싶네..
JM
2010-05-25 12:01:43
@lqez, 아 그게 아니라요. 파이썬으로 일단 짜고. 그리고 타이트한 루프 부분을 C++ 로 인라인해 짠 겁니다. 예를 들어 for i in xrange(N): sum += A[i]; 를 for(int i = 0; i < N; ++i) sum += i; 로 바꿔서 짜 넣은 거지요. 컴파일러들 인라인 매우 잘 해줍니다. :-) 제가 자세하게 안써서 헷갈리셨군요. ^^;
@dg, 동건이도 따라서 헷갈렸군. ㅋㅋ
dg
2010-05-25 18:56:58
따라서 햇갈린건 아니고, lqez님 커멘트 전에도 C++의 인라인 함수로 생각했어.. -_-;; 파이썬인건 알았는데.. 나는 파이썬의 ctypes을 사용해서 윈도우에서 dll이나 리눅스에서 so파일에 있는 함수를 호출하는 짓을 해본지라....
JM
2010-05-26 00:59:25
@dg, ah.. I see. But I hear using ctypes is a bit painful.. Did you try using SWIG or something else?
dg
2010-05-31 11:57:02
I didn't know about SWIG or something. I used ctypes to call few functions I wrote in C from Python, and I found it is handy. But ctypes is not suitable for calling foreign functions in general, because it does not check number of arguments or type of arguments (in Windows, it "tries" to check number of argument by examining stack, and you can specify number and type of arguments to be checked using argtypes). And there are problems about calling convention in x86. cdll uses cdecl and windll uses stdcall in Windows but I don't know how to call using fastcall. And I don't recommend ctypes to call C++ function unless it is not a member function of a class and declared in extern "C".
JM
2010-06-01 14:34:07
@dg, wow thanks for the detailed comment. Yeah I'd rather not write Python interfacing code using ctypes directly.. I wonder how SWIG solves these kind of problems.

댓글 남기기