-1

So i build this code in .Net Core and it works wonderfully good But when i use this code in .NetFramework It's really slow and not picking up the pace

    static int wichOneIsBigger(Random rand, int number)
    {
        //65-90 (Upper) 97 122 (Lower)
        Exception e = new Exception("Number must be 0 for lower case and 1 for upper case");
        if (number != 0 && number != 1)
            throw e;
        else
            if (number == 0)
            return rand.Next(97, 122);
        else
            return rand.Next(65 - 90);
    }
    static void Main()
    {
        string txt = null;

        Random rand = null;
        int length = 0;
        Console.WriteLine("Please type a number that is above 1: ");

        txt = Console.ReadLine();
        length = Convert.ToInt32(txt);

        string[] words = new string[length];
        for (int i = 0; i < length; i++)
        {
            rand = new Random();
            int characters = rand.Next(4, 10);
            int randCharacter = 0;
            int wichOne = 0;
            string word = "";
            for (int num = 0; num < characters; num++)
            {
                wichOne = rand.Next(0, 1);
                randCharacter = wichOneIsBigger(rand, wichOne);

                word += Convert.ToChar(randCharacter);
            }
            words[i] = word;
        }

        foreach (string item in words)
            Console.WriteLine(item);

        GC.Collect();
        Console.ReadKey();
    }

when im using .Net Core it's giving me this result for example the length is 5:

gaerantd
dxunjxtw
gevnyiqb
xhpsvfqu
gnnkaulxg

But when im using .Net Framework it giving me this

aist
aist
aist
aist
aist

Why in the .Net Framework it wont picking up the pace? I didnt programmed for 2.5 years and i dont remmember .Net Framework being this slow

By the way do you guys have any suggestion of my code? Can i write it better and etc?

Thanks for the help :)

Pinkie Pie
  • 61
  • 4
  • So far code posted in the question is simply wrong due to incorrect usage of `Random`. If after you fix that and still have a problem - [edit] post with code that looks reasonable and also post your performance numbers so it is clear what "slowness" you are talking about (show code that you use to measure execution time too - that one frequently get written wrong too). – Alexei Levenkov Dec 29 '17 at 04:06
  • 1
    What is slow? You are recreating `Random` each time which (pauses to find the fine manual) https://msdn.microsoft.com/en-us/library/h343ddh9(v=vs.110).aspx *Initializes a new instance of the Random class, using a time-dependent default seed value ... The default seed value is derived from the system clock and has finite resolution. As a result, different Random objects that are created in close succession by a call to the default constructor will have identical default seed values and, therefore, will produce identical sets of random numbers.* – ta.speot.is Dec 29 '17 at 04:06
  • 1
    If anything the .NET Framework is too fast because you're able to create all those `Random` instances with the same seed‽ – ta.speot.is Dec 29 '17 at 04:07
  • I happened upon this question from a Google search to compare the speed of .Net Framework to.Net Core. Either the title should be changed to reflect the duplicatness of the question or a performances aspect should be added, which is what the question is about. Yes, Random is used incorrectly, but that's not the question. The linked duplicated question has little to do with the question. – Chuck Conway May 19 '20 at 15:44

2 Answers2

0

This line: rand = new Random(); in your loop written like that is likely not going to be random, I don't think it has anything to do with slowness? Change your code to something like this:

static Random rand = new Random();
static void Main()
{
     //..etc
}
Kevin DiTraglia
  • 25,746
  • 19
  • 92
  • 138
  • It's because the rand = new Random(); was in the loop now it works fine I go kill myself bye JK thanks for the help i didnt knew that will slow it down but it acctually make sense since its making the same random over and over again so it "reset it self" i guess It's still not as fast as .Net Core but it's faster than what it was on the start lol – Pinkie Pie Dec 29 '17 at 04:11
-1

Most likely because .Net Core is a stripped down version of .Net so many packages are excluded. For most applications if you can make do with .Net Core it will likely be faster.

Max
  • 123
  • 8
  • I see but in .net framework languages like VB.NET And other it work slow aswell So if i want to make for example a program like that im restricted to use .net core? In .net core i cant use window forms and alot of refrences btw – Pinkie Pie Dec 29 '17 at 04:03