I’m wondering if I should favor one approach over the other in terms or clarity, readability performance and whatever other qualifications that makes a code “better”.
Let’s take this simple example:
rand int a;
rand int b;
constraint a_b_c {
solve a before b;
b == a + 3;
}
In a sense, b
isn’t really random.
I can calculate it in post_randomize with invoking any new randomization technique.
So, in that case, what are the pros and cons of calculating it in a constraint block vs post_randomize?
Key point: you can have many constraints in a class, but you only get one post_randomize
method. I would base the choice on how confident you are that that behavior of b
is the ony thing you’ll ever need. If very confident, then the post_randomize
method seems simple and nicely aligned with code intent. If, on the other hand, you have a set of b
behaviors in mind, the ability to selectively enable/disable among a set of constraints that assign b
differently will be useful.
It all depends on your perspective. I see that both a and b are random variables, and the constraint establishes a relationship between them. The solve a before b;
construct does not do anything here.
But let’s say you need to extend this class and add a constraint to b like
{ b % 2 == 1;} // must be an odd number
Then you are stuck if you had used post_randomize()
.
I would save post_randomize()
for things that are difficult to express as a constraint.
Does it take the same compute time to calculate a derivative value in a constraint like it does in post_randomize?
I can only guess that a constraint might take more compute time than in post_randomize. But it’s hard to say if it’s a one-time overhead at the compilation step, once per construction of the class, or at each call to randomize. You would have to do some experiments.