void ClientLauncher::speed_tests() { // volatile to avoid some potential compiler optimisations volatile static s16 temp16; volatile static f32 tempf; static v3f tempv3f1; static v3f tempv3f2; static std::string tempstring; static std::string tempstring2; tempv3f1 = v3f(); tempv3f2 = v3f(); tempstring = std::string(); tempstring2 = std::string(); { infostream << "The following test should take around 20ms." << std::endl; TimeTaker timer("Testing std::string speed"); const u32 jj = 10000; for (u32 j = 0; j < jj; j++) { tempstring = ""; tempstring2 = ""; const u32 ii = 10; for (u32 i = 0; i < ii; i++) { tempstring2 += "asd"; } for (u32 i = 0; i < ii+1; i++) { tempstring += "asd"; if (tempstring == tempstring2) break; } } } infostream << "All of the following tests should take around 100ms each." << std::endl; { TimeTaker timer("Testing floating-point conversion speed"); tempf = 0.001; for (u32 i = 0; i < 4000000; i++) { temp16 += tempf; tempf += 0.001; } } { TimeTaker timer("Testing floating-point vector speed"); tempv3f1 = v3f(1, 2, 3); tempv3f2 = v3f(4, 5, 6); for (u32 i = 0; i < 10000000; i++) { tempf += tempv3f1.dotProduct(tempv3f2); tempv3f2 += v3f(7, 8, 9); } } { TimeTaker timer("Testing std::map speed"); std::map<v2s16, f32> map1; tempf = -324; const s16 ii = 300; for (s16 y = 0; y < ii; y++) { for (s16 x = 0; x < ii; x++) { map1[v2s16(x, y)] = tempf; tempf += 1; } } for (s16 y = ii - 1; y >= 0; y--) { for (s16 x = 0; x < ii; x++) { tempf = map1[v2s16(x, y)]; } } } { infostream << "Around 5000/ms should do well here." << std::endl; TimeTaker timer("Testing mutex speed"); JMutex m; u32 n = 0; u32 i = 0; do { n += 10000; for (; i < n; i++) { m.Lock(); m.Unlock(); } } // Do at least 10ms while(timer.getTimerTime() < 10); u32 dtime = timer.stop(); u32 per_ms = n / dtime; infostream << "Done. " << dtime << "ms, " << per_ms << "/ms" << std::endl; } }
void SpeedTests(IrrlichtDevice *device) { /* Test stuff */ //test(); //return 0; /*TestThread thread; thread.Start(); std::cout<<"thread started"<<std::endl; while(thread.IsRunning()) sleep(1); std::cout<<"thread ended"<<std::endl; return 0;*/ { std::cout<<"Testing floating-point conversion speed"<<std::endl; u32 time1 = device->getTimer()->getRealTime(); tempf = 0.001; for(u32 i=0; i<10000000; i++){ temp16 += tempf; tempf += 0.001; } u32 time2 = device->getTimer()->getRealTime(); u32 fp_conversion_time = time2 - time1; std::cout<<"Done. "<<fp_conversion_time<<"ms"<<std::endl; //assert(fp_conversion_time < 1000); } { std::cout<<"Testing floating-point vector speed"<<std::endl; u32 time1 = device->getTimer()->getRealTime(); tempv3f1 = v3f(1,2,3); tempv3f2 = v3f(4,5,6); for(u32 i=0; i<40000000; i++){ tempf += tempv3f1.dotProduct(tempv3f2); tempv3f2 += v3f(7,8,9); } u32 time2 = device->getTimer()->getRealTime(); u32 dtime = time2 - time1; std::cout<<"Done. "<<dtime<<"ms"<<std::endl; } { std::cout<<"Testing core::map speed"<<std::endl; u32 time1 = device->getTimer()->getRealTime(); core::map<v2s16, f32> map1; tempf = -324; for(s16 y=0; y<500; y++){ for(s16 x=0; x<500; x++){ map1.insert(v2s16(x,y), tempf); tempf += 1; } } for(s16 y=500-1; y>=0; y--){ for(s16 x=0; x<500; x++){ tempf = map1[v2s16(x,y)]; } } u32 time2 = device->getTimer()->getRealTime(); u32 dtime = time2 - time1; std::cout<<"Done. "<<dtime<<"ms"<<std::endl; } { std::cout<<"Testing mutex speed"<<std::endl; u32 time1 = device->getTimer()->getRealTime(); u32 time2 = time1; JMutex m; m.Init(); u32 n = 0; u32 i = 0; do{ n += 10000; for(; i<n; i++){ m.Lock(); m.Unlock(); } time2 = device->getTimer()->getRealTime(); } // Do at least 10ms while(time2 < time1 + 10); u32 dtime = time2 - time1; u32 per_ms = n / dtime; std::cout<<"Done. "<<dtime<<"ms, " <<per_ms<<"/ms"<<std::endl; } //assert(0); }
void SpeedTests() { { dstream<<"The following test should take around 20ms."<<std::endl; TimeTaker timer("Testing std::string speed"); const u32 jj = 10000; for(u32 j=0; j<jj; j++) { tempstring = ""; tempstring2 = ""; const u32 ii = 10; for(u32 i=0; i<ii; i++){ tempstring2 += "asd"; } for(u32 i=0; i<ii+1; i++){ tempstring += "asd"; if(tempstring == tempstring2) break; } } } dstream<<"All of the following tests should take around 100ms each." <<std::endl; { TimeTaker timer("Testing floating-point conversion speed"); tempf = 0.001; for(u32 i=0; i<4000000; i++){ temp16 += tempf; tempf += 0.001; } } { TimeTaker timer("Testing floating-point vector speed"); tempv3f1 = v3f(1,2,3); tempv3f2 = v3f(4,5,6); for(u32 i=0; i<10000000; i++){ tempf += tempv3f1.dotProduct(tempv3f2); tempv3f2 += v3f(7,8,9); } } { TimeTaker timer("Testing core::map speed"); core::map<v2s16, f32> map1; tempf = -324; const s16 ii=300; for(s16 y=0; y<ii; y++){ for(s16 x=0; x<ii; x++){ map1.insert(v2s16(x,y), tempf); tempf += 1; } } for(s16 y=ii-1; y>=0; y--){ for(s16 x=0; x<ii; x++){ tempf = map1[v2s16(x,y)]; } } } { dstream<<"Around 5000/ms should do well here."<<std::endl; TimeTaker timer("Testing mutex speed"); JMutex m; m.Init(); u32 n = 0; u32 i = 0; do{ n += 10000; for(; i<n; i++){ m.Lock(); m.Unlock(); } } // Do at least 10ms while(timer.getTime() < 10); u32 dtime = timer.stop(); u32 per_ms = n / dtime; dstream<<"Done. "<<dtime<<"ms, " <<per_ms<<"/ms"<<std::endl; } }
collisionMoveResult collisionMoveSimple(Map *map, IGameDef *gamedef, f32 pos_max_d, const core::aabbox3d<f32> &box_0, f32 dtime, v3f &pos_f, v3f &speed_f) { collisionMoveResult result; v3f oldpos_f = pos_f; v3s16 oldpos_i = floatToInt(oldpos_f, BS); /* Calculate new position */ pos_f += speed_f * dtime; /* Collision detection */ // position in nodes v3s16 pos_i = floatToInt(pos_f, BS); /* Collision uncertainty radius Make it a bit larger than the maximum distance of movement */ f32 d = pos_max_d * 1.1; // A fairly large value in here makes moving smoother //f32 d = 0.15*BS; // This should always apply, otherwise there are glitches assert(d > pos_max_d); /* Calculate collision box */ core::aabbox3d<f32> box = box_0; box.MaxEdge += pos_f; box.MinEdge += pos_f; core::aabbox3d<f32> oldbox = box_0; oldbox.MaxEdge += oldpos_f; oldbox.MinEdge += oldpos_f; /* If the object lies on a walkable node, this is set to true. */ result.touching_ground = false; /* Go through every node around the object */ s16 min_x = (box_0.MinEdge.X / BS) - 2; s16 min_y = (box_0.MinEdge.Y / BS) - 2; s16 min_z = (box_0.MinEdge.Z / BS) - 2; s16 max_x = (box_0.MaxEdge.X / BS) + 1; s16 max_y = (box_0.MaxEdge.Y / BS) + 1; s16 max_z = (box_0.MaxEdge.Z / BS) + 1; for(s16 y = oldpos_i.Y + min_y; y <= oldpos_i.Y + max_y; y++) for(s16 z = oldpos_i.Z + min_z; z <= oldpos_i.Z + max_z; z++) for(s16 x = oldpos_i.X + min_x; x <= oldpos_i.X + max_x; x++) { try{ // Object collides into walkable nodes MapNode n = map->getNode(v3s16(x,y,z)); if(gamedef->getNodeDefManager()->get(n).walkable == false) continue; } catch(InvalidPositionException &e) { // Doing nothing here will block the object from // walking over map borders } core::aabbox3d<f32> nodebox = getNodeBox(v3s16(x,y,z), BS); /* See if the object is touching ground. Object touches ground if object's minimum Y is near node's maximum Y and object's X-Z-area overlaps with the node's X-Z-area. Use 0.15*BS so that it is easier to get on a node. */ if( //fabs(nodebox.MaxEdge.Y-box.MinEdge.Y) < d fabs(nodebox.MaxEdge.Y-box.MinEdge.Y) < 0.15*BS && nodebox.MaxEdge.X-d > box.MinEdge.X && nodebox.MinEdge.X+d < box.MaxEdge.X && nodebox.MaxEdge.Z-d > box.MinEdge.Z && nodebox.MinEdge.Z+d < box.MaxEdge.Z ){ result.touching_ground = true; } // If object doesn't intersect with node, ignore node. if(box.intersectsWithBox(nodebox) == false) continue; /* Go through every axis */ v3f dirs[3] = { v3f(0,0,1), // back-front v3f(0,1,0), // top-bottom v3f(1,0,0), // right-left }; for(u16 i=0; i<3; i++) { /* Calculate values along the axis */ f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[i]); f32 nodemin = nodebox.MinEdge.dotProduct(dirs[i]); f32 objectmax = box.MaxEdge.dotProduct(dirs[i]); f32 objectmin = box.MinEdge.dotProduct(dirs[i]); f32 objectmax_old = oldbox.MaxEdge.dotProduct(dirs[i]); f32 objectmin_old = oldbox.MinEdge.dotProduct(dirs[i]); /* Check collision for the axis. Collision happens when object is going through a surface. */ bool negative_axis_collides = (nodemax > objectmin && nodemax <= objectmin_old + d && speed_f.dotProduct(dirs[i]) < 0); bool positive_axis_collides = (nodemin < objectmax && nodemin >= objectmax_old - d && speed_f.dotProduct(dirs[i]) > 0); bool main_axis_collides = negative_axis_collides || positive_axis_collides; /* Check overlap of object and node in other axes */ bool other_axes_overlap = true; for(u16 j=0; j<3; j++) { if(j == i) continue; f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[j]); f32 nodemin = nodebox.MinEdge.dotProduct(dirs[j]); f32 objectmax = box.MaxEdge.dotProduct(dirs[j]); f32 objectmin = box.MinEdge.dotProduct(dirs[j]); if(!(nodemax - d > objectmin && nodemin + d < objectmax)) { other_axes_overlap = false; break; } } /* If this is a collision, revert the pos_f in the main direction. */ if(other_axes_overlap && main_axis_collides) { speed_f -= speed_f.dotProduct(dirs[i]) * dirs[i]; pos_f -= pos_f.dotProduct(dirs[i]) * dirs[i]; pos_f += oldpos_f.dotProduct(dirs[i]) * dirs[i]; result.collides = true; } } } // xyz return result; }