I wrote this within a C# Windows Form. I prefer this over a straight console app.
using System.Runtime.CompilerServices;
using System.Text;
/*
Instructions:
What is the time required to execute the same program on each generation of the processor?
Mr. Hammad selects a program and executes each CPU generation separately. The selected program
has 500 instructions containing 60% CPU instructions and 40% IO instructions. On the first
generation of CPU, each CPU instruction takes approximately 1 second to execute whereas each
I/O instruction takes 0.5 times more to execute as compared to CPU instructions.
On the second generation of the processor, the execution of CPU and IO instructions
is improved by 15% and 10% respectively as compared to the first generation. Similarly, on the
third generation of the processor, the execution of CPU and IO instructions is further improved
by 15% and 10% respectively as compared to the second generation.
Considering the above-given scenario, answer the following questions:
a) What is the time required to execute the same program on each generation of the processor?
b) What percentage of performance improvement will be there for each processor generation over the previous generation when executing the same program?
Solution Notes:
Question a: The following code calculates and displays the first 50 generations.
Question b: Each generation improves by 15% and 10% as given. I am guessing the intent
of the question is how much improvement there is compared to the initial runtime.
*/
namespace WizantExample1
{
public partial class Form1 : Form
{
/* Constant Values */
// Total number of instructions
private const int totalInstructions = 500;
// 60% CPU Instructions
private const int numberOfCPUInstructions = (int)(totalInstructions * 0.6);
// 40% IO Instructions.
private const int numberOfIOInstructions = (int)(totalInstructions * 0.4);
// Processing time of the first generation
private const double secondsPerCPUInstruction_Initial = 1;
private const double secondsPerIOInstruction_Initial = secondsPerCPUInstruction_Initial * 1.5;
// Change Per Generation
private const double ratioPerGenerationCPU = 0.15;
private const double ratioPerGenerationIO = 0.10;
/* Class variables */
// Track time to process CPU instructions
private double secondsPerCPUInstruction_Current = 0;
// Track time to process IO Instructions
private double secondsPerIOInstruction_Current = 0;
// Only update the display once all processing has finished
private StringBuilder outputText = new StringBuilder();
// Constructor
public Form1()
{
InitializeComponent();
}
// Called when Run button is clicked
private void RunButton_Click(object sender, EventArgs e)
{
// Initialize variables
this.InitializeVariables();
// Display initial values
outputText.AppendLine("Total Instructions: " + Form1.totalInstructions);
outputText.AppendLine("CPU Instructions: " + Form1.numberOfCPUInstructions);
outputText.AppendLine("IO Instructions: " + Form1.numberOfIOInstructions);
outputText.AppendLine();
this.printStatus(generationNumber: 0);
// Loop Through the generations
for (int generation = 1; generation < 50; generation++)
{
// Update
this.UpdateGeneration();
this.printStatus(generation);
}
// Update output field
this.ScrollOutput.Text = this.outputText.ToString();
}
// Set initial values
private void InitializeVariables()
{
// Output Init
this.outputText = new StringBuilder();
// Time Required for First Generation
this.secondsPerCPUInstruction_Current = 1;
// IO Takes 0.5 times longer than a CPU instruction
this.secondsPerIOInstruction_Current = this.secondsPerCPUInstruction_Current * 1.5;
}
// Update the calculations for each generation
private void UpdateGeneration()
{
this.secondsPerCPUInstruction_Current *= 1.0 - Form1.ratioPerGenerationCPU;
this.secondsPerIOInstruction_Current *= 1.0 - Form1.ratioPerGenerationIO;
}
// Display the current calculations
private void printStatus(int generationNumber)
{
double cpuPercentage = 0;
double ioPercentage = 0;
// Do not print differences on first generation
if (generationNumber > 0)
{
cpuPercentage = 1.0 - this.secondsPerCPUInstruction_Current / Form1.secondsPerCPUInstruction_Initial;
ioPercentage = 1.0 - this.secondsPerIOInstruction_Current / Form1.secondsPerIOInstruction_Initial;
}
this.outputText.AppendLine("Generation: " + generationNumber);
this.outputText.AppendLine(" CPU seconds: " + this.secondsPerCPUInstruction_Current.ToString("N5"));
this.outputText.AppendLine(" CPU increase: " + cpuPercentage.ToString("P"));
this.outputText.AppendLine(" IO seconds: " + this.secondsPerIOInstruction_Current.ToString("N5"));
this.outputText.AppendLine(" IO increase: " + ioPercentage.ToString("P"));
this.outputText.AppendLine();
}
}
}