This question already has answers here:
Is there a way to count the number of IL instructions executed?
(5 answers)
Closed 7 years ago.
I want to mesure how much instruction are executed in method example.
int a=0;
functionTest();
a=getCountInstruction() // return the number of instruction executed until now.
Their any way to do it using Profiler or some Classes ?
You have to add increase counter when you invoke your method
static int a=0;
functionTest()
{
a++;
//your code
}
Related
This question already has answers here:
Shortest way to check for null and assign another value if not
(12 answers)
Closed 2 years ago.
Is it possible to execute a function within a short statement if the statement is true?
Something like that:
myObject.subObject != null ?? Db.LoadReferences(myObject.subObject); // ORMLite function
yes:
if(myObject.subObject != null) Db.LoadReferences(myObject.subObject);
At just 1 single character more than your original.
This question already has answers here:
Captured variable in a loop in C#
(10 answers)
Thread parameters being changed
(2 answers)
Closed 2 years ago.
I would like to add onClick listeners to my buttons in Unity, C#.
I have an array of these buttons. I am trying to loop through that array, and for each button, I am adding an onClick listener which calls a function that takes an index parameter.
In that function, I print out the paramater that the function has received.
However, when I click on one of these buttons, I get 10 as an output every time.
for(int i = 0; i < roleButtons.Length; ++i) {
roleButtons[i].onClick.AddListener(delegate { changeEquipment(i); });
}
void changeEquipment(int index) {
Debug.Log(index); //Here I got 10 as an output after every single click.
}
This question already has answers here:
Which is the correct C# infinite loop, for (;;) or while (true)? [closed]
(20 answers)
Closed 2 years ago.
I find empty "for(;;)" statement in someone else code, and I can't figure out why it is used like this.
try
{
for (;;)
{
It is an infinite loop, just like: while (true)
This question already has answers here:
pre Decrement vs. post Decrement
(2 answers)
Closed 6 years ago.
I was doing some C# practice and decided to make a basic function to sum the contents of an integer array.
Originally I wrote my code as follows:
if(index == 0)
return toSum[index];
else
return toSum[index] + sum(toSum, index--);
Now that code resulted in a StackOverFlow exception. This made no sense to me; surely this is how one would do a summation? Turns out the problem was in the index--. When I changed it to index - 1 it worked out fine, thus I was wondering why is that the case? My understanding is that it is simply a shorthand for index = index-1. I was wondering if anyone could explain the reason behind this behavior.
Post-decrement operator returns the value before decrementing, so in your case the index will never be 0 and the function won't stop calling itself and you'll get a stack overflow. You want to write --index instead. It will return the value after decrementing then.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
In C# is a for(;;) safe and what does it really do?
So i recently came across something ive never seen before..
for (; ; )
{
}
What is exactly happening when the feilds are left blank like that?
It's an infinite loop.
Somewhere inside there should be a break; statement, or possibly an exception thrown in order for control to pass beyond the loop.
You could also achieve the same thing (probably more obviously) by doing
while (true)
{
// do stuff
}
This is an infinite loop, almost equivalent to a while(true) loop.
The break condition is not there in between the two semicolons, therefore, it must be there somewhere in the loop body.
That's an infinite for loop.