From: "Carlo E. Prelz" Date: 2012-12-24T18:32:46+09:00 Subject: Re: Ruby game server woes Subject: Ruby game server woes Date: Mon 24 Dec 12 08:45:33AM +0900 Quoting Na Na (lists@ruby-forum.com): > So essentially, my question is this - given all these streams of > thoughts, are there any insights you can give me on how to approach > this? I lack enough knowledge to make an informed choice. Did you actually try to add a C extension? It is something that has to be learned. Not exactly easy, but once you've learned the mechanism, and when it is properly used, results are quite important. You do not describe your algorithm. In my experience, a C extension is best justified when I have bursts of heavy maths. In that case I generally create a C-defined class, which is instantiated at the beginning of the run. At instantiation I pass those variables that allow me to properly allocate all the memory that will be needed. I also often precompute tables: let's say that you need very often the value of the tangent of a specific range of angle values: allocate say, one million floats and fill them with the appropriate values. I have noticed that I gain the most with math, but also when I have to deal with very large loops: the Ruby loops are wonderful and marvellously flexible, but when you are in search of performance, it is worth the effort to move your big loops to C. An example? Take this simple Ruby script, that computes the tangent table I mentioned above: --8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<-- FVAL=0.0 TVAL=90.0 NVALS=1024*1024 step=(TVAL-FVAL)/(NVALS-1).to_f data=Array::new(NVALS) ang=FVAL NVALS.times do |i| data[i]=Math::tan(ang) ang+=step end --8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<-- I execute it here on my laptop (i5-2410M CPU @ 2.30GHz) and I get these times: real 0m0.297s user 0m0.289s sys 0m0.007s The same computation in C: --8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<-- #include #include #define FVAL 0.0 #define TVAL 90.0 #define NVALS (1024*1024) void main(void) { float step=(TVAL-FVAL)/(float)(NVALS-1),ang,*data=malloc(sizeof(float)*NVALS); int i; for(ang=FVAL,i=0;i