Structures vs Components, an unfair challenge?
A post over in the cf-talk list mentioned that using CreateObject was a lot slower than native datatypes in Coldfusion, well of course it is I thought, but I wanted to see HOW slow they compare, and what the difference would be between them.
So to this aid I did a script that looped, creating objects and timing it, in steps of 50 up to 2000 objects. The code to measure this is as follows:
<!--- lets loop this crazyness--->
<!--- arrays to fill--->
<!--- create a struct --->
<cfset stItem.name = "Bob" & i>
<cfset ArrayAppend(stArray,stItem)>
<cfset tArrayTotal = GetTickCount() - tStartArray>
<!--- create an component --->
<cfset oItem = CreateObject('component', 'Person')>
<cfset oItem.setname("Bob" & i )>
<cfset oItem.setage(20)>
<cfset ArrayAppend(oArray,oItem)>
<cfset tObjectTotal = GetTickCount() - tStartObject>
<!--- log the two times for this iteration --->
<cffile action="append" file="#expandpath("createObject.log")#" output="#x#,#tArrayTotal#,#tObjectTotal#">
I added some timing info after each iteration and was adding the objects (a struct and a simple Person component) to a log file.
So you dont think that the Person component is very heavy, I did the barest component possible, you might even say its Duck-like!
The results for the component are pretty uniform, but what surprised me was how little speed it takes to create structures. Obviously this is the case, since they are basically a HashMap and in ColdFusion a very long running primitive (i.e. been around a while and optimised)
These results of course are of course unfair, you wouldnt create 2000 odd objects, you might create 5-10 of them as services and keep them cached, but what about beans?
Here is the chart of the results:
