Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7434231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:48:08+00:00 2026-05-29T09:48:08+00:00

I am currently implementing a runtime (i.e. a collection of functions) for a formulas

  • 0

I am currently implementing a runtime (i.e. a collection of functions) for a formulas language. Some formulas need a context to be passed to them and I created a class called EvaluationContext which contains all properties I need access to at runtime.

Using ThreadLocal<EvaluationContext> seems like a good option to make this context available to the runtime functions. The other option is to pass the context as a parameter to the functions that need it.

I prefer using ThreadLocal but I was wondering if there is any performance penalty as opposed to passing the evaluation context via method parameters.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:48:09+00:00Added an answer on May 29, 2026 at 9:48 am

    I created the program below and it is faster to use parameters rather than the ThreadLocal field.

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace TestThreadLocal
    {
      internal class Program
      {
        public class EvaluationContext
        {
          public int A { get; set; }
          public int B { get; set; }
        }
    
        public static class FormulasRunTime
        {
          public static ThreadLocal<EvaluationContext> Context = new ThreadLocal<EvaluationContext>();
    
          public static int SomeFunction()
          {
            EvaluationContext ctx = Context.Value;
            return ctx.A + ctx.B;
          }
    
          public static int SomeFunction(EvaluationContext context)
          {
            return context.A + context.B;
          }
        }
    
    
    
        private static void Main(string[] args)
        {
    
          Stopwatch stopwatch = Stopwatch.StartNew();
          int N = 10000;
          Task<int>[] tasks = new Task<int>[N];
          int sum = 0;
          for (int i = 0; i < N; i++)
          {
            int x = i;
            tasks[i] = Task.Factory.StartNew(() =>
                                                     {
                                                       //Console.WriteLine("Starting {0}, thread {1}", x, Thread.CurrentThread.ManagedThreadId);
                                                       FormulasRunTime.Context.Value = new EvaluationContext {A = 0, B = x};
                                                       return FormulasRunTime.SomeFunction();
                                                     });
            sum += i;
          }
          Task.WaitAll(tasks);
    
          Console.WriteLine("Using ThreadLocal: It took {0} millisecs and the sum is {1}", stopwatch.ElapsedMilliseconds, tasks.Sum(t => t.Result));
          Console.WriteLine(sum);
          stopwatch = Stopwatch.StartNew();
    
          for (int i = 0; i < N; i++)
          {
            int x = i;
            tasks[i] = Task.Factory.StartNew(() =>
            {
              return FormulasRunTime.SomeFunction(new EvaluationContext { A = 0, B = x });
            });
    
          }
          Task.WaitAll(tasks);
    
          Console.WriteLine("Using parameter: It took {0} millisecs and the sum is {1}", stopwatch.ElapsedMilliseconds, tasks.Sum(t => t.Result));
          Console.ReadKey();
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello everyone I'm currently implementing a simple programming language for learning experience but I'm
I'm currently implementing some image processing code for Android. I'm aware of the memory
i'm currently implementing a localized website. I've created a custom ResourceProvider + Factory for
I'm currently implementing a pageview counter on some webpages using the Google Analytics API.
I am currently implementing SiteMinder for the site, which looks for a key called
I am currently implementing the INotifyCollectionChanged interface for a collection with generally quite critical
I'm currently implementing a copy constructor for a Linked List class. When I create
I'm currently implementing a raytracer. Since raytracing is extremely computation heavy and since I
I'm currently implementing a JavaScript library that keeps track of the history of changes
I'm currently implementing a very complex tree structure to allow for near-instant data access,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.