Wednesday, April 3, 2013
Scala: Call by Name
Functional languages typically implement the call by name evaluation strategy natively. There are some special use cases where such an evaluation strategy may boost performance and one of the examples was given here.
The case in consideration is how log statements within code can be made more efficient while being clean. Typically log level determines if the log statement actually does something in the code.
For example, lets say you wanted to put the following log statement in the code:
logger.info("ok" + "to" + "concatenate" + "string" + "to" + "log" +"message")
If the check for the log level is done inside the log method, then by the time the check is done, the string concatenation would already have been performed. That would lead to bad performance. So the solution would be to wrap the code above with a check on the log level:
if (logger.isEnabledFor(Logger.INFO)) {
// Ok to log now.
logger.info("ok" + "to" + "concatenate" + "string" + "to" + "log" +"message");
}
In a language like Scala, this would be achieved by using a call by name argument to the logger method. The call by name argument is evaluated (pretty much like a parameter-less function call) only when needed. It is different from a lazy evaluation in that it is evaluated each time it is needed (call it lazy with no memory!).
Here is the code quoted from the above article:
def log(level: Level, message: => String) = if (logger.level.intValue >= level.intValue) logger.log(level, msg)
Another excellent example can be seen here.
About Tukeys
Tukeys provides consulting and software services to companies in healthcare and communications that are dealing with technical or scaling challenges. Please reach out to me on LinkedIn.
An interesting quite from John Tukey, who was an American mathematician (1915-2000) -
An approximate answer to the right problem is worth a good deal more than an exact answer to an approximate problem.
Today maths and statistics form the basis of how software is developed and used around us.
About Me
Medhavi I have over 20 years of experience in healthcare, communications and AI.
I have a passion for defining innovative applications, creating product/technology strategy and solving challenging problems. My product mindset is helpful to any company to find patterns, reduce costs, and gain efficiencies.
I was the VP of Engineering at Vibrent Health, where I led the web, mobile, API and data products that supported one of the largest NIH programs - All of Us Research Program. Our system enrolled almost a million users and collected data from EHRs, surveys and wearables. Before that, I worked at hCentive, a pioneer in health exchanges.
As the CTO of 3CLogic, I helped create the first PC and web-only contact center solution that implemented PBX and CRM functionality as an affordable SaaS solution. As the Chief Architect of NexTone, I was instrumental in the development of SIP and SIP based products. SIP and associated protocols form the basis of modern WebRTC APIs.
If you are a growth company or invest in growing companies that are facing technical or scaling challenges, I can be a tremendous help. Contact me on LinkedIn. View my complete profile.
Featured Post
Random Forest Analysis of U.S. National Epidemiological Survey on Alcohol and Related Conditions (NESARC) data
In 2001/2002, the National Institute on Alcohol Abuse and Alcoholism (NIAAA) conducted the National Epidemiologic Survey on Alcohol and Related Conditions.