a puzzle for timing method
-
public interface IncDec { void Increment(); void Decrement(); } public class MyIncDec : IncDec { private int x; public MyIncDec(int x) { this.x = x; } public void Increment() { this.x++; } public void Decrement() { this.x--; } }
Part 1: Implement a class which can be used to measure how long each invocation of the Increment and Decrement method takes (in milliseconds) and prints out this information. The class should fit in more or less transparently with existing clients of the IncDec interface. Part 2: Imagine you need to do something similar (timing of method calls) for many places in an application. What other ideas come to mind, e.g. could you propose other ideas for getting timing information more conveniently? I got this question need to answer, it is simple, just something confused to me. hope anyone can help me. btw, what "The class should fit in more or less transparently with existing clients of the IncDec interface. " means? -
public interface IncDec { void Increment(); void Decrement(); } public class MyIncDec : IncDec { private int x; public MyIncDec(int x) { this.x = x; } public void Increment() { this.x++; } public void Decrement() { this.x--; } }
Part 1: Implement a class which can be used to measure how long each invocation of the Increment and Decrement method takes (in milliseconds) and prints out this information. The class should fit in more or less transparently with existing clients of the IncDec interface. Part 2: Imagine you need to do something similar (timing of method calls) for many places in an application. What other ideas come to mind, e.g. could you propose other ideas for getting timing information more conveniently? I got this question need to answer, it is simple, just something confused to me. hope anyone can help me. btw, what "The class should fit in more or less transparently with existing clients of the IncDec interface. " means? -
public interface IncDec { void Increment(); void Decrement(); } public class MyIncDec : IncDec { private int x; public MyIncDec(int x) { this.x = x; } public void Increment() { this.x++; } public void Decrement() { this.x--; } }
Part 1: Implement a class which can be used to measure how long each invocation of the Increment and Decrement method takes (in milliseconds) and prints out this information. The class should fit in more or less transparently with existing clients of the IncDec interface. Part 2: Imagine you need to do something similar (timing of method calls) for many places in an application. What other ideas come to mind, e.g. could you propose other ideas for getting timing information more conveniently? I got this question need to answer, it is simple, just something confused to me. hope anyone can help me. btw, what "The class should fit in more or less transparently with existing clients of the IncDec interface. " means?dragon_yue wrote:
I got this question need to answer, it is simple, just something confused to me. hope anyone can help me. btw, what "The class should fit in more or less transparently with existing clients of the IncDec interface. " means?
I suppose your teacher (this is homework, right?) means to give you a hint. If you create a new class that inherits MyIncDec and call the base classes methods, you can use this class instead of the orignal and have a place to put your performance timing code without disturbing MyIncDec's existing users.
Standards are great! Everybody should have one!
-
public interface IncDec { void Increment(); void Decrement(); } public class MyIncDec : IncDec { private int x; public MyIncDec(int x) { this.x = x; } public void Increment() { this.x++; } public void Decrement() { this.x--; } }
Part 1: Implement a class which can be used to measure how long each invocation of the Increment and Decrement method takes (in milliseconds) and prints out this information. The class should fit in more or less transparently with existing clients of the IncDec interface. Part 2: Imagine you need to do something similar (timing of method calls) for many places in an application. What other ideas come to mind, e.g. could you propose other ideas for getting timing information more conveniently? I got this question need to answer, it is simple, just something confused to me. hope anyone can help me. btw, what "The class should fit in more or less transparently with existing clients of the IncDec interface. " means?Hi, this is a stupid assignment. In order to make it a realistic problem, one would need to measure a method containing hundred or more instructions to start with, not a single increment. The code required to transparently measure the duration of an increment will probably use a derived class, virtual methods, and a StopWatch. As such it will be much slower than the original increment itself, so the result is bound to be inaccurate (on top of being utterly irrelevant). :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
dragon_yue wrote:
I got this question need to answer, it is simple, just something confused to me. hope anyone can help me. btw, what "The class should fit in more or less transparently with existing clients of the IncDec interface. " means?
I suppose your teacher (this is homework, right?) means to give you a hint. If you create a new class that inherits MyIncDec and call the base classes methods, you can use this class instead of the orignal and have a place to put your performance timing code without disturbing MyIncDec's existing users.
Standards are great! Everybody should have one!
its not homework, I konw its simple. thanks your answer. do you think the new class inherit from interface or that class. which one is better
-
Hi, this is a stupid assignment. In order to make it a realistic problem, one would need to measure a method containing hundred or more instructions to start with, not a single increment. The code required to transparently measure the duration of an increment will probably use a derived class, virtual methods, and a StopWatch. As such it will be much slower than the original increment itself, so the result is bound to be inaccurate (on top of being utterly irrelevant). :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
I understand. it is stupid one. thanks