Jump to content


Photo

A Question of Speed


  • This topic is locked This topic is locked
15 replies to this topic

#1 banov

banov

    GMC Member

  • GMC Member
  • 1170 posts
  • Version:Unknown

Posted 07 October 2011 - 03:24 AM

Hey all.

I've been working a lot with optimizing my current project lately, and a big question that often pops up for me is what functions work the fastest. For example, in many places in my game I could use many/any of the following functions interchangeably:

place_meeting
position_meeting
instance_place
instance_position
place_free
place_empty

Do we know which of these are fastest, slowest, etc? When you have a lot of objects performing function like these, often in the step event, it becomes important. On that same note, do there exist other collision checking alternatives that are faster even than these? Collision_circle, collision_line, etc?

Also: which is faster, draw_sprite or draw_background?

------------------------------------------
Results:
Done by banov, using GM 8.1 on a Windows 7 32 bit machine
Spoiler

Edited by banov, 01 December 2011 - 07:16 PM.

  • 1

#2 mads2194

mads2194

    GMC Member

  • GMC Member
  • 376 posts
  • Version:GM:Studio

Posted 07 October 2011 - 11:38 AM

Hey all.

I've been working a lot with optimizing my current project lately, and a big question that often pops up for me is what functions work the fastest. For example, in many places in my game I could use many/any of the following functions interchangeably:

place_meeting
position_meeting
instance_place
instance_position
place_free
place_empty


Why don`t you try seeing for yourself? ^^

room_speed=99999;
var time;
time = current_time;
repeat(10000000)
     place_meeting(32,32,obj);
show_message("Time spent :"+string(current_time-time)+" ms"); // 1 sec = 1000 ms

Just compare the average time of these functions and see which is lower.
  • 0

#3 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 21919 posts
  • Version:GM:Studio

Posted 07 October 2011 - 11:41 AM

This is not exactly a answer, but it certainly is information relevant to your question and so i suggest that you read this article by Mike Dailly :

http://dailly.blogsp...-detection.html

I would also agree that testing these functions is a simple task... so why not do it and then you tell us what you find? My guess is that most of the place_ functions will be pretty much the same (and fast!) as long as the masks are rectangular and NOT precise, with the instance_ and position_ functions being marginally slower.
  • 0

#4 banov

banov

    GMC Member

  • GMC Member
  • 1170 posts
  • Version:Unknown

Posted 18 October 2011 - 06:48 AM

Okay, so, I did it. Using GM 8.1 on a Windows 7 32 bit machine, I got these relationships between the 6 functions I mentioned. FROM FASTEST TO SLOWEST:

place_free
place_empty (1.35741737 times slower)
instance_position (1.46533435 times slower)
position_meeting (1.47732513 times slower)
instance_place (1.58032282 times slower)
place_meeting (1.69546502 times slower)

Interesting, non?

If there are any more significant collisions checks I should run through, feel free to ask here, I guess. Would be relatively simple to add them in now.

The code I used:
Spoiler


And here's a link to the .gm81 if you feel like playing around yourself: http://www.box.net/s...0m8t7002591cc6r
I had two boxes size 16x16 with origin at 0,0 placed in the same space, with one checking the collisions.

Edited by banov, 18 October 2011 - 06:52 AM.

  • 2

#5 Reefpirate

Reefpirate

    GMC Member

  • GMC Member
  • 335 posts

Posted 22 October 2011 - 09:34 PM

I for one will find this testing you did useful. Thanks!
  • 0

#6 JonathanPzone

JonathanPzone

    GMC Member

  • GMC Member
  • 1215 posts
  • Version:GM:Studio

Posted 23 October 2011 - 05:06 AM

<science>


Interesting enough to be bookmarked for future reference!
  • 0

#7 drt_t1gg3r

drt_t1gg3r

    GMC Member

  • GMC Member
  • 3459 posts
  • Version:GM8.1

Posted 31 October 2011 - 11:21 PM

Time spent creating 1000 objects:
16 ms: - 1 sec = 1000ms
-------------------------------------
-------------------------------------
place_free 100000 times:
1092 ms: - 1 sec = 1000ms
-------------------------------------
place_empty 100000 times:
561 ms: - 1 sec = 1000ms
-------------------------------------
instance_position 100000 times:
1997 ms: - 1 sec = 1000ms
-------------------------------------
position_meeting 100000 times:
2013 ms: - 1 sec = 1000ms
-------------------------------------
instance_place 100000 times:
717 ms: - 1 sec = 1000ms
-------------------------------------
place_meeting 100000 times:
702 ms: - 1 sec = 1000ms
-------------------------------------
-------------------------------------
place_empty: 561
place_meeting: 702
instance_place: 717
place_free: 1092
instance_position: 1997
position_meeting: 2013
Running on Window's 7 64bit AMD Athlon II X2 250u Processor 1.60 GHz
additional findings show that place_meeting and instance_place run close for 2 and 3 and switch back and forth throughout 1000 tests
place_empty by far is the winner after a 1000 tests. But if in a crunch it is only a fragment slower to use either place-meeting or instance_place
and the total losers were instance_position and position_meeting 5x slower than place_empty
  • 0

#8 banov

banov

    GMC Member

  • GMC Member
  • 1170 posts
  • Version:Unknown

Posted 01 November 2011 - 05:58 PM

Thanks, drt! I think it's really interesting how different your results were from mine... did you use the same code as me?

Edited by banov, 01 November 2011 - 06:02 PM.

  • 0

#9 drt_t1gg3r

drt_t1gg3r

    GMC Member

  • GMC Member
  • 3459 posts
  • Version:GM8.1

Posted 02 November 2011 - 01:10 AM

EDIT: sorry double posted

Edited by drt_t1gg3r, 02 November 2011 - 01:13 AM.

  • 0

#10 drt_t1gg3r

drt_t1gg3r

    GMC Member

  • GMC Member
  • 3459 posts
  • Version:GM8.1

Posted 02 November 2011 - 01:12 AM

for the code I just kept repeating the time = current_time thing above each call and then forward the results to the debug_message so I could have it all in one sheet. After this I did a repeat 1000 times kind a thing and took the average and it still came up with the same results.
this was the actual code
Spoiler

Edited by drt_t1gg3r, 22 November 2011 - 12:57 PM.

  • 0

#11 norisak

norisak

    GMC Member

  • GMC Member
  • 131 posts
  • Version:GM8

Posted 19 November 2011 - 06:13 PM

I tried drt's code and here is the results on 64bit Windows 7 with Intel i5-2500k @ 3.3GHz

Time spent creating 1000 objects:
0
 ms: - 1 sec = 1000ms
--------------------------
-----------
--------------------------
-----------
place_free 100000 times:
405 ms: - 1 sec = 1000ms
--------------------------
-----------
place_empty 100000 times:
1030 ms: - 1 sec = 1000ms
--------------------------
-----------
instance_position 100000 times:
1326 ms: - 1 sec = 1000ms
--------------------------
-----------
position_meeting 100000 times:
1326 ms: - 1 sec = 1000ms
--------------------------
-----------
instance_place 100000 times:
1544 ms: - 1 sec = 1000ms
--------------------------
-----------
place_meeting 100000 times:
1545 ms: - 1 sec = 1000ms
--------------------------
-----------
--------------------------
-----------
place_free: 405
place_empty: 1030
instance_position: 1326
position_meeting: 1326
instance_place: 1544
--------------------------
-----------
Rotating 360 at 1000 length:
4243 ms: - 1 sec = 1000ms
--------------------------
-----------
--------------------------
-----------

Edited by norisak, 19 November 2011 - 06:13 PM.

  • 0

#12 drt_t1gg3r

drt_t1gg3r

    GMC Member

  • GMC Member
  • 3459 posts
  • Version:GM8.1

Posted 22 November 2011 - 12:53 PM

I wonder why it's such a difference between the ratings of my test and Norisak's test? I mean looking at place_empty for my test came up with 561ms and for the same function it almost doubles in time for Norisak's test even though for his test he had place_free at 405ms and for my test I had place_free at more than double the speed at 1092. Can anyone explain why such a difference?
  • 0

#13 filulilus

filulilus

    GMC Member

  • GMC Member
  • 966 posts
  • Version:GM:Studio

Posted 23 November 2011 - 05:00 PM

May depend on what sprites you where using and if they where set to precies or not precies collision checking? o:

Oh... I just saw that the instances are placed randomly in the room, that "will sure" effect the results.
Since I asume the collision checking is done in more then 1 step.

Note: I'm not 100% sure about this but I guess this is some what how it works...

It should checks if the instance is even close to the main instance before going in to "heavy calulations"
that way it can sort out a lot of instances and keep the frame rate low (unless all instances are stacked on eachother):
if x + right_side < other.x - other.left_side {
//not even close, don't have to do some box collision checking
} else {
//go on with the collision checking...
}
At least this is how I made my 3D collision system :tongue: (which is very slow since its done with only arrays, for loops and a lot of "if"s so don't ask me to release it :unsure:)

Edited by filulilus, 23 November 2011 - 06:03 PM.

  • 0

#14 banov

banov

    GMC Member

  • GMC Member
  • 1170 posts
  • Version:Unknown

Posted 01 December 2011 - 07:12 PM

I don't think creating instances at random places makes for very consistent results. In my test I made sure that every check was "true," because for functions that have to return information about the colliding object (like the id), this will likely slow it down. Run your tests again and make sure that every check gets a perfectly fair chance--either all should result in "true" or all should result in "false." Any other mixture and you're muddying the results.

With that in mind, I generated a new set of results.


In this test, both participating objects were SOLID and at the exact same position in the room with the same size hitbox (so all collision tests were TRUE)
Spoiler


And in this test, the objects were kept a distance apart and both non-solid so all checks were FALSE (notice how every check is about 1.5 times faster!)
Spoiler


So the main change is instance_place is faster than place_empty when both are true, but not when both are false! A "true" collision check is always slower than any "false" collision check, and usually by quite a lot!

SCIENCE!
  • 0

#15 filulilus

filulilus

    GMC Member

  • GMC Member
  • 966 posts
  • Version:GM:Studio

Posted 02 December 2011 - 08:56 AM

^ Thanks for confirming that! Did you use none precise collision detection?
  • 0

#16 theALCH3MIST

theALCH3MIST

    GMC Member

  • New Member
  • 92 posts
  • Version:GM8

Posted 10 December 2011 - 04:39 PM

Very useful information, thanks. I couldn't help noticing the differences between the benchmarks though - are these functions opimised for multi-core processing, or is it raw processor crunching power that accounts for the difference?
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users