gamblers-dice | A terrible idea | Analytics library
kandi X-RAY | gamblers-dice Summary
kandi X-RAY | gamblers-dice Summary
A terrible idea, now real.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of gamblers-dice
gamblers-dice Key Features
gamblers-dice Examples and Code Snippets
Community Discussions
Trending Discussions on gamblers-dice
QUESTION
I've created a library for fun called GamblersDice. I'm trying to micro-optimize it but unsure if I'm doing it right. What I want is to use a reference to a global Random object when creating the die. The reason I believe it's not working is that testing GamblersDie(random, int) and GamblersDie(ref random, int) take nearly the same time over 10,000,000 iterations (test project is in the repo). If you don't want to scan through the repo, here are my constructors:
...ANSWER
Answered 2018-Jul-24 at 14:31What do variables/parameters hold? They hold values. What is the nature of the values they store? For Value types (struct
) they store the value itself.
But for Reference types (class
), they store a reference; they don't store the object itself, that's located elsewhere.
It is these that are copied-in in the default pass-by-value parameter passing in C#. And copying a reference is not the same as creating a copy of the object to which it refers. So, even without ref
, you're dealing with a single object both inside and outside the method. Any changes to the object will, obviously, be visible externally.
You only need ref
if a) you're going to reassign the parameter and that reassignment should be externally visible or b) you're mutating a Value type and that mutation should be externally visible.
And, in case the above isn't clear enough, I will explicitly state it here - Random
is a Reference type.
Bonus reading: The Truth About Value Types by Eric Lippert, which you may not be ready for but does help dispel some common myths that you may pick up along the way.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install gamblers-dice
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page