I have a simple program written in C# and I ran it in two laptops same specs but one has Windows 8 and the other has Windows 10 and I measure the time in milliseconds and get the difference between the start and the end but in windows 8 it takes approx 1k milliseconds while in windows 10 it takes approx 15k milliseconds. Why this huge difference?
This is the code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
long milliseconds = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
Console.WriteLine(milliseconds);
for(int i = 0; i < 100000000; i++)
{
if(i%10000==0)
Console.WriteLine(i);
}
long milliseconds2 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
Console.WriteLine(milliseconds2);
Console.WriteLine("Diff " + (milliseconds2-milliseconds));
Console.ReadLine();
}
}
}
Related
I'm trying to calculate a big number using Mathf.Pow() but when I place a breakpoint it shows infinity, I already tried using System.Numerics.BigInteger but it shows Big Integer cannot display infinity
Here's my code
using System;
using System.Numerics;
namespace TestConsole
{
class Program
{
static void Main(string[] args)
{
//43
BigInteger res = new BigInteger(MathF.Pow(43, 27));
Console.WriteLine(res);
}
}
}
As Jeremy said
You could try BigInteger.Pow instead
I used BigInteger res = BigInteger.Pow(43,27); and that worked, thanks!
I am currently in the process of writing a c# program in VS where it reads the holding registers of a Panasonic KW9M-A Power Meter using Modbus-RTU. The holding register i'm trying to read is:
00A4H to 00A5H; Unsigned 32bit
Note: I wrote this in Console app. I used NModbus4 by Maxwe11
Idk what i'm missing since i'm a beginner in programming. Can someone please assist me? Thank you in advance :)
Here is what i have so far:
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console_ye
{
class Program
{
public static void Main(string[] args)
{
SerialPort serialPort = new SerialPort("COM13", 115200, Parity.Odd, 8, StopBits.One);
serialPort.Open();
Console.WriteLine("This is the beginning: ");
string hex_add = "0x00A4";
ushort dec_add = Convert.ToUInt16(hex_add, 16);
Console.WriteLine("Value of hex: " + hex_add);
Console.WriteLine("Value of ushort: " + dec_add);
byte slaveId = 1;
ushort startAddress = dec_add;
ushort numberOfPoints = 8;
IModbusMaster masterRTU = ModbusSerialMaster.CreateRtu (serialPort);
ushort[] ushortArray = masterRTU.ReadHoldingRegisters(slaveId, startAddress, numberOfPoints);
Console.WriteLine("Here " + ushortArray[0]);
foreach (ushort item in ushortArray)
{
Console.WriteLine(string.Join ("\n", item));
}
}
}
}
Don't know anything about NModbus4, but as shown in the sample program, you'll probably need to include one or more of the following in your program.
using Modbus.Data;
using Modbus.Device;
using Modbus.Utility;
using Modbus.Serial;
You should correct the line:
Console.WriteLine(string.Join ("\n", item));
to
Console.WriteLine(item);
Then it'll be workable.
In the company I work at we have a bunch of web applications in which we display in the website's footer some information useful for the developers (such as the assembly version).
Performance wise (I'm not caring for the UX aspect of it), would it be a bad idea to show the memory usage by calling GC.GetTotalMemory(false)? Should I worry having this call performed on every request?
I got curious when I saw your question since it was not the regular stuff, and decided to give it a try.
Created a simple web site and in default.aspx filled almost 1 GB of small objects (10 million byte arrays with 64 bytes each) into the heap, then called GC.GetTotalMemory 100 times and before each time by adding a new object so that if there is any cached value, it would be obsolete.
The average execution time for GC.GetTotalMemory(false) was 0,000687 milliseconds!
I have a Win 7 64 bit 2 cpu (8 core total) HP with 8 GB RAM.
I think, your developers will have a good metric down there :)
Default.aspx.cs:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
private List<object> objects = new List<object>();
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10000000; i++)
{
objects.Add(new byte[64]);
}
double averageDuration = 0;
for (int x = 0; x < 100; x++)
{
objects.Add(new byte[64]);
Stopwatch sw = Stopwatch.StartNew();
Response.Write("Total memory: " + GC.GetTotalMemory(false) + " / ");
sw.Stop();
averageDuration += sw.Elapsed.TotalMilliseconds;
Response.Write("Duration: " + sw.Elapsed.TotalMilliseconds + "<br/>");
}
Response.Write("Average Duration: " + (averageDuration / 100));
}
}
I'm attempting to do Project Euler problem #4 in C#. The problem I'm having is that when the code runs a console window briefly appears and then goes away. I don't know what the problem could be as I'm relatively new to programming.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
for (int i = 1000; i > 100; i--)
for (int j = 1000; j > 100; j--)
PalCheck(i * j);
}
static void PalCheck(int original)
{
var reversed = new string(Convert.ToString(original).ToCharArray().Reverse().ToArray());
if (Convert.ToString(original) == reversed)
Console.WriteLine(original);
Console.ReadKey();
}
}
}
The code seems to be stuck at the line
Console.ReadKey() as at this line of code, the program is waiting for some input key.
Since you have not used any message before ReadKey(), you don't realize that the program is waiting for some input and not stuck.
Move Console.ReadKey() after PalCheck(i * j) and you should see the output on the console screen.
I want to create a c# program that will look in a folder for files. If the files are found then I want to start a program. If the file is not there then I want to program to sleep for 30 minutes and look in the folder again. I want to keep doing it maybe 10 times and if it still doesn’t find the file then exit the program. I wrote the if part but I need help on the else part. This is what I have so far.
using System;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Diagnostics;
class Program
{
static void Main()
{
// See if this file exists in the SAME DIRECTORY.
if (File.Exists(#"C:\name.txt"))
{
Process.Start(#"C:\bulkload.bat");
}
else
{
}
}
}
Untested, for guidance purpose only.
for (int i = 0; i < 10; i++)
{
if (File.Exists(#"C:\name.txt"))
{
Process.Start(#"C:\bulkload.bat");
return;
}
else //no need of else block really.
{
Thread.Sleep(30 * 60 * 1000);
}
}