由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 关于c++的效率再给个例子
相关主题
包子问使用C templates Sort data的问题请问haskell中的underscore
搞不定,不得不问,一维数组跟二维数组的问题问个java8问题
用root跑程序更快c的问题
强迫症爱好者进来做题了Amazon的Fibonacci题
如何查看一个程序/进程使用了哪些cpu?最近变硬那家的面经
Python擂台:算24点这两道leetcode题有更好的答案吗?
求最佳解法有人做projecteuler吗?
python一问,怎么实现这个函数被鄙视了, c++基础知识
相关话题的讨论汇总
话题: template话题: time话题: fib话题: fibonacci话题: data
进入Programming版参与讨论
1 (共1页)
b*******s
发帖数: 5216
1
template
long long factorial()
{
return factorial(NUM - 1) * NUM;
}
然后对0,1特例化一下
现在问,运行时计算 factorial<20> 需要多少次运算?
20次乘法?
答案是一次内存读操作就行了
w***g
发帖数: 5958
2
连我都要说你这个是cheating。C++的模板元编程的对手是haskell。回去想个更好的例
子来。

【在 b*******s 的大作中提到】
: template
: long long factorial()
: {
: return factorial(NUM - 1) * NUM;
: }
: 然后对0,1特例化一下
: 现在问,运行时计算 factorial<20> 需要多少次运算?
: 20次乘法?
: 答案是一次内存读操作就行了

b*******s
发帖数: 5216
3
想象一下你写的关于数值计算的类库
能提供O(1)的阶乘算法

【在 b*******s 的大作中提到】
: template
: long long factorial()
: {
: return factorial(NUM - 1) * NUM;
: }
: 然后对0,1特例化一下
: 现在问,运行时计算 factorial<20> 需要多少次运算?
: 20次乘法?
: 答案是一次内存读操作就行了

b*******s
发帖数: 5216
4
我只是在介绍语言特性,c++用来写库还是很爽的

【在 w***g 的大作中提到】
: 连我都要说你这个是cheating。C++的模板元编程的对手是haskell。回去想个更好的例
: 子来。

b*******s
发帖数: 5216
5
我换constexpr怎么样
这个总不是meta programming了吧

【在 w***g 的大作中提到】
: 连我都要说你这个是cheating。C++的模板元编程的对手是haskell。回去想个更好的例
: 子来。

g*********e
发帖数: 14401
6

这个是说编译的时候把他算好了吗?

【在 b*******s 的大作中提到】
: template
: long long factorial()
: {
: return factorial(NUM - 1) * NUM;
: }
: 然后对0,1特例化一下
: 现在问,运行时计算 factorial<20> 需要多少次运算?
: 20次乘法?
: 答案是一次内存读操作就行了

b*******s
发帖数: 5216
7
是的,编译期决定,效率比c高

【在 g*********e 的大作中提到】
:
: 这个是说编译的时候把他算好了吗?

n****l
发帖数: 1739
8
that is pretty much useless if n varies in run time.
a better approach is to use plain c and build up a look up table.
this is a good example for c++ "expert" to mind masturbating on
features that do not matter.
plus, template is really ugly looking.

【在 b*******s 的大作中提到】
: 是的,编译期决定,效率比c高
b*******s
发帖数: 5216
9
这个设计简直是灾难

【在 n****l 的大作中提到】
: that is pretty much useless if n varies in run time.
: a better approach is to use plain c and build up a look up table.
: this is a good example for c++ "expert" to mind masturbating on
: features that do not matter.
: plus, template is really ugly looking.

N******K
发帖数: 10202
10
数学运算库 不用template 就是搞笑

【在 n****l 的大作中提到】
: that is pretty much useless if n varies in run time.
: a better approach is to use plain c and build up a look up table.
: this is a good example for c++ "expert" to mind masturbating on
: features that do not matter.
: plus, template is really ugly looking.

相关主题
Python擂台:算24点请问haskell中的underscore
求最佳解法问个java8问题
python一问,怎么实现这个函数c的问题
进入Programming版参与讨论
a*********a
发帖数: 3656
11
with some template tricks, the lookup table can be built up to a constant N
at compile time. Run time for any n
【在 n****l 的大作中提到】
: that is pretty much useless if n varies in run time.
: a better approach is to use plain c and build up a look up table.
: this is a good example for c++ "expert" to mind masturbating on
: features that do not matter.
: plus, template is really ugly looking.

g*****g
发帖数: 34805
12
You can put it in DB and look up a table 100 times you memory can hold any
time. What's the point?

N

【在 a*********a 的大作中提到】
: with some template tricks, the lookup table can be built up to a constant N
: at compile time. Run time for any n
a*********a
发帖数: 3656
13
We are talking about shaving cpu cycles and your suggestion is to introduce
IO?
I know netflix does not mind making its users wait for seconds or minutes,
but in my business, 1 millisecond slower than competitors means annihilation.

【在 g*****g 的大作中提到】
: You can put it in DB and look up a table 100 times you memory can hold any
: time. What's the point?
:
: N

g*****g
发帖数: 34805
14
It doesn't matter what business, you cant preload the data and compiler
trick is the only way?
Should Nasdaq hardcode all ticks to avoid some IO? That's just ridiculous
argument.

introduce
annihilation.

【在 a*********a 的大作中提到】
: We are talking about shaving cpu cycles and your suggestion is to introduce
: IO?
: I know netflix does not mind making its users wait for seconds or minutes,
: but in my business, 1 millisecond slower than competitors means annihilation.

n****l
发帖数: 1739
15
no more template tricks, please. what about the compiling error of
template code? is that nearly sane now?

N

【在 a*********a 的大作中提到】
: with some template tricks, the lookup table can be built up to a constant N
: at compile time. Run time for any n
b*******s
发帖数: 5216
16
the err msgs are as bad as ever

【在 n****l 的大作中提到】
: no more template tricks, please. what about the compiling error of
: template code? is that nearly sane now?
:
: N

T********i
发帖数: 2416
17
Once you get used to it, it is not that bad any more.

【在 b*******s 的大作中提到】
: the err msgs are as bad as ever
b*******s
发帖数: 5216
18
我已经习惯了,但是的确是丑陋了一点

【在 T********i 的大作中提到】
: Once you get used to it, it is not that bad any more.
f******y
发帖数: 2971
19
factorial<20>本来就是一个常数,C++和Java都只需要读一下内存。

【在 b*******s 的大作中提到】
: template
: long long factorial()
: {
: return factorial(NUM - 1) * NUM;
: }
: 然后对0,1特例化一下
: 现在问,运行时计算 factorial<20> 需要多少次运算?
: 20次乘法?
: 答案是一次内存读操作就行了

a*********a
发帖数: 3656
20
preloading sometimes is not an option Say your program crash in the middle
of a trading session, each second you waste reloading the DB is money.
now if you are preloading, you now can't do 100 times the memory can hold
can you? So if it is a limited set of fixed data, compile once and be done
vs read every time on start up, which one is better?

【在 g*****g 的大作中提到】
: It doesn't matter what business, you cant preload the data and compiler
: trick is the only way?
: Should Nasdaq hardcode all ticks to avoid some IO? That's just ridiculous
: argument.
:
: introduce
: annihilation.

相关主题
Amazon的Fibonacci题有人做projecteuler吗?
最近变硬那家的面经被鄙视了, c++基础知识
这两道leetcode题有更好的答案吗?报个groupon的面经……下周onsite2个公司,顺求bless
进入Programming版参与讨论
g*****g
发帖数: 34805
21
So you don't need to recompile and redeploy every time you add a tick. How
hard is it to understand that ? Separation of code and data is basic
principle. And while I can't preload 100 times memory of data, I can pick
what data to cache and at what time, with simple algorithm like LRU, can you?
Trying to avoid crash is the right way to go, not saving a few seconds after
crash. I can't believe this question is even being asked.

【在 a*********a 的大作中提到】
: preloading sometimes is not an option Say your program crash in the middle
: of a trading session, each second you waste reloading the DB is money.
: now if you are preloading, you now can't do 100 times the memory can hold
: can you? So if it is a limited set of fixed data, compile once and be done
: vs read every time on start up, which one is better?

l*********s
发帖数: 5409
22
yes for sure.

【在 n****l 的大作中提到】
: no more template tricks, please. what about the compiling error of
: template code? is that nearly sane now?
:
: N

l*********s
发帖数: 5409
23
I think it is.

【在 b*******s 的大作中提到】
: 我换constexpr怎么样
: 这个总不是meta programming了吧

f******y
发帖数: 2971
24
这个真的是你自己不行而已。

【在 n****l 的大作中提到】
: no more template tricks, please. what about the compiling error of
: template code? is that nearly sane now?
:
: N

k**********g
发帖数: 989
25

有 static_assert 好不好

【在 n****l 的大作中提到】
: no more template tricks, please. what about the compiling error of
: template code? is that nearly sane now?
:
: N

a*********a
发帖数: 3656
26
hmm, I thought long and hard how can a simple Fibonacci violate separation
of data and code.
Then I realized, that we were demonstrating how static computation can be
used to optimize run time. I said you can create a static table of fib
for n imposed separation of data and code so algorithms to compute Fibonacci at
compile time are outlawed.
Very very impressive PPT skills you possess there! Your legal team should
have asked your advices, perhaps then you guys wouldn't have lost the net
neutrality case.
Now since you mentioned avoiding crash. Let s see. We all know that
Fibonacci overflows an integer at about n=50. With template techniques,
simple specialize fib<50> to a false static assertion, all attempt to access
an overflowing fibonacci would produce compile time errors! There is no
possibility of run time overflow due to this CODE. On the hand, you DATA
approach must be have run time checks for validity, which in best cases
introduces unnecessary branching (albeit most likely an extremely
predictable branch), in worst case leads to crashes.
You also mentioned compilation cost. If you were familiar with template
techniques, you would already have seen that what I proposed already
sacrificed the 0 run time of typical template compile time computation. That
means I would be using forced instantiation, where all the fib templates
with (n is
not inlined as 8 but have to deref an address. This C file is pretty
compiled and
and forgot about.

you?
after

【在 g*****g 的大作中提到】
: So you don't need to recompile and redeploy every time you add a tick. How
: hard is it to understand that ? Separation of code and data is basic
: principle. And while I can't preload 100 times memory of data, I can pick
: what data to cache and at what time, with simple algorithm like LRU, can you?
: Trying to avoid crash is the right way to go, not saving a few seconds after
: crash. I can't believe this question is even being asked.

b*******s
发帖数: 5216
27
连template都不是

【在 l*********s 的大作中提到】
: I think it is.
l*********s
发帖数: 5409
28
没这个东东concept就玩不了,而concept对template非常有用。

【在 b*******s 的大作中提到】
: 连template都不是
g*****g
发帖数: 34805
29
And who told you Fib function overflow at 50? You can't fit the result in an
Integer, doesn't mean that you can't fit it as a String. If you are going to
provide a website for people to query Fib, you think they'll never input
more than 50?
Your argument proves exactly why compiler trick is so limited it's barely
useful. You can do 50, and I don't see a problem doing 5000. 100 times more
than you can do as I said.

【在 a*********a 的大作中提到】
: hmm, I thought long and hard how can a simple Fibonacci violate separation
: of data and code.
: Then I realized, that we were demonstrating how static computation can be
: used to optimize run time. I said you can create a static table of fib
: for n: imposed separation of data and code so algorithms to compute Fibonacci at
: compile time are outlawed.
: Very very impressive PPT skills you possess there! Your legal team should
: have asked your advices, perhaps then you guys wouldn't have lost the net
: neutrality case.

d********f
发帖数: 43471
30
你跟java程序员较什么劲,人家脑子里根本没有内存好不好

【在 b*******s 的大作中提到】
: template
: long long factorial()
: {
: return factorial(NUM - 1) * NUM;
: }
: 然后对0,1特例化一下
: 现在问,运行时计算 factorial<20> 需要多少次运算?
: 20次乘法?
: 答案是一次内存读操作就行了

1 (共1页)
进入Programming版参与讨论
相关主题
被鄙视了, c++基础知识如何查看一个程序/进程使用了哪些cpu?
报个groupon的面经……下周onsite2个公司,顺求blessPython擂台:算24点
求问一个题求最佳解法
好挫的F家面经python一问,怎么实现这个函数
包子问使用C templates Sort data的问题请问haskell中的underscore
搞不定,不得不问,一维数组跟二维数组的问题问个java8问题
用root跑程序更快c的问题
强迫症爱好者进来做题了Amazon的Fibonacci题
相关话题的讨论汇总
话题: template话题: time话题: fib话题: fibonacci话题: data