Saturday, February 4, 2023
HomeJavaScriptTesting Efficiency Overhead Of Creating Java Courses In Lucee CFML

Testing Efficiency Overhead Of Creating Java Courses In Lucee CFML


Some of the highly effective options of ColdFusion is the truth that it’s constructed on high of Java; and, at any time, we will attain down into the Java layer for added performance. The standard manner by which we do that is to name createObject("java") and go in a Java class title. Traditionally, I’ve tended to cache the returned Java class worth, working beneath the belief that createObject() had quite a lot of overhead. However, I do not assume I ever based mostly this assumption on any concrete proof. As such, I wished to carry out a trite efficiency exploration concerning the createObject() operate in Lucee CFML.

As with all low-level efficiency experiments, this must be taken with a grain of salt seeing as I am working it on my machine with no manufacturing visitors. That mentioned, my purpose right here is not to see absolute numbers however extra to get a way of relative efficiency between two completely different approaches.

To do this out, I’ll experiment with a Static methodology on Java’s Lengthy class: .toString(). In my first loop, I’ll name createObject() inside each iteration. Then, in my second loop, I’ll use a cached occasion of the LongClass.

ASIDE: Once I say “occasion” right here, it’s kind of fuzzy – it is the occasion of the class definition, not an instantiated occasion of the category. This has to do with the ColdFusion abstraction of Java.

Notice that within the following code, I am outputting one worth inside every loop as a way to guarantee that the ColdFusion compiler is not making an attempt to “optimize” the loop out of the code (seeing because it would not actually do something).

<cfscript>

	trialCount = 5;
	iterations = 1000000;

	echo( "Trials: #trialCount#, Iterations: #numberFormat( iterations )# <br />" );

	// ------------------------------------------------------------------------------- //
	// ------------------------------------------------------------------------------- //

	loop instances = trialCount {

		echo( "<br />" );

		// -- TEST ONE --: Calling the createObject() operate each time I must entry one
		// of the underlying Java strategies.
		timer
			label = "Calling createObject() each time"
			kind = "define"
			{

			loop instances = iterations {

				worth = createObject( "java", "java.lang.Lengthy" )
					.toString( randRange( 1, 100 ), 36 )
				;

			}

			echo( "Final worth: #worth#" );

		}

		// -- TEST TWO --: Caching the results of createObject() operate, after which reusing
		// the cached class once I must entry one of many underlying Java strategies.
		LongClass = createObject( "java", "java.lang.Lengthy" );

		timer
			label = "Reusing Cached Class"
			kind = "define"
			{

			loop instances = iterations {

				worth = LongClass.toString( randRange( 1, 100 ), 36 );

			}

			echo( "Final worth: #worth#" );

		}

	}

</cfscript>

Every trial is working 1,000,000 loop iterations for every check. I needed to jack this variety of fairly excessive earlier than I began to see any efficiency distinction. And, after we run this ColdFusion code with 1 million iterations, we get the next output:

Testing showing that the average duration of each approach was within about 70ms (over 1 million iterations).

As you may see, there’s some efficiency overhead to calling the createObject() operate. Nevertheless, it’s extraordinarily small, exhibiting solely a few 70ms mixture overhead over the course of 1 million invocations. Basically, there is no such thing as a distinction.

I am glad I did this as a result of caching the createObject() worth provides complexity to my ColdFusion code. And, seeing that there is not any significant overhead in calling the createObject() operate, I’ll cease making an attempt to cache these values. Clearly, it was a untimely optimization; and, I am blissful to be rid of it.

Wish to use code from this submit?
Take a look at the license.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments