Question

In my function (or task), I have a constant string that is only used inside that method.

What is the best way to define it (for performance):

  1. const static string stuff = "stuff";
  2. const string stuff = "stuff";
  3. static string stuff = "stuff";
  4. string stuff = "stuff";

Example on EDA Playground: http://www.edaplayground.com/s/4/1090

Was it helpful?

Solution

  • const will prevent future writes, IEEE std 1800-2012 § 6.20.6 "Const constants" states "... a const can be set during simulation ..." which suggests that it is up to the vender to decide if there should be any optimization for performance.
  • static will put the variable into shares memory. It could help or hurt performance depending on the simulation scenario. Actual performance impact is simulator specific, so you will need to run your own benchmarks. IEEE std 1800-2012 § 6.21 "Scope and lifetime" for more.

For a small project, the performance impact will be negligible. For large projects, performance needs to be split into categories: memory usage, and memory access time. static variables can have trade-off smaller memory footprint (shared memory) and longer look-up times (memory address of the static variable can be faraway for the rest of the object data). const is unlikely to add any negative performance.

Easiest way to get some basic performance data is to end the simulation with $finish(2). See IEEE std 1800-2012 Table 20-1—Diagnostics for $finish. This will report the simulation time, location, and statistics about the memory and CPU time used in simulation if the simulator is following the standard.

With the provided example using ModelSim 10.1d, all combinations reported the same memory usage. Run time was only effected by the number of calls the print method, not the const/static attribute.

If I had to guess, the performance would be (ordered best to worse):

  1. const string in a static method
  2. string in a static method
  3. const static string
  4. static string
  5. const string
  6. string
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top