Counting Cache Misses in Data Structure

Arnab Chakraborty
Updated on 10-Aug-2020 08:01:20

359 Views

In algorithm analysis we count the operations and steps. This is basically justified when computer takes more time to perform an operation than they took to fetch the data needed for that operation. Nowadays the cost of performing an operation is significantly lower than the cost of fetching data from memory.The run time of many algorithms is dominated by the number of memory references (number of cache misses) rather than by the number of operations. So, when we will try to desing some algorithms, we have to focus on reducing not only the number of operations but also the number ... Read More

Operation Counts Method in Algorithm

Arnab Chakraborty
Updated on 10-Aug-2020 07:57:52

4K+ Views

There are different methods to estimate the cost of some algorithm. One of them by using the operation count. We can estimate the time complexity of an algorithm by choosing one of different operations. These are like add, subtract etc. We have to check how many of these operations are done. The success of this method depends on our ability to identify the operations that contribute most of the time complexity.Suppose we have an array, of size n [0 to n - 1]. Our algorithm will find the index of largest element. We can estimate the cost by counting number ... Read More

Difference Between Last and LastOrDefault in LINQ

Nizamuddin Siddiqui
Updated on 08-Aug-2020 11:37:18

1K+ Views

Both Last() and LastOrDefault() will fetch the last occurrence of a value. But the major difference between Last() and LastOrDefault() is that Last() will throw an exception if there is no result data for the supplied criteria whereas LastOrDefault() will return the default value (null) if there is no result data.Use Last() when we knew the sequence will have at least one element. Use LastOrDefault() when we are not sure about the data.Example Live Demousing System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; namespace ConsoleApp {    public class Student {       public int Id { get; set; }   ... Read More

Use of yield return in C#

Nizamuddin Siddiqui
Updated on 08-Aug-2020 11:33:12

3K+ Views

Yield keyword helps to do custom stateful iteration over a collection. Meaning when we use yield keyword the control moves back and forth from the caller function to source and vice versa.Example Live Demousing System; using System.Collections.Generic; namespace DemoApplication {    class Program {       static List numbersList = new List {          1, 2, 3, 4, 5       };       public static void Main() {          foreach(int i in RunningTotal()) {             Console.WriteLine(i);          }          Console.ReadLine(); ... Read More

Return Null from a Generic Method in C#

Nizamuddin Siddiqui
Updated on 08-Aug-2020 11:30:47

2K+ Views

Generics allows us to define a class with placeholders for the type of its fields, methods, parameters, etc. Generics replace these placeholders with some specific type at compile time. A generic can be defined using angle brackets . A primary limitation of collections is the absence of effective type checking. This means that you can put any object in a collection because all classes in the C# programming language extend from the object base class.Also, we cannot simply return null from a generic method like in normal method. Below is the error that a generic method will throw if we ... Read More

Copy Entire Contents of a Directory in C#

Nizamuddin Siddiqui
Updated on 08-Aug-2020 11:28:54

2K+ Views

While copying the entire contents of directory, it is more important that we have copy its sub directories and the related files.ExampleLet us consider demo source directory having sub directories and files like below. Below is the demo target directory which is empty initially.using System; using System.IO; namespace DemoApplication {    class Program {       public static void Main() {          string sourceDirectory = @"d:\DemoSourceDirectory";          string targetDirectory = @"d:\DemoTargetDirectory";          DirectoryInfo sourceDircetory = new DirectoryInfo(sourceDirectory);          DirectoryInfo targetDircetory = new DirectoryInfo(targetDirectory);          CopyAll(sourceDircetory, ... Read More

Replace Line Breaks in a String in C#

Nizamuddin Siddiqui
Updated on 08-Aug-2020 11:21:11

2K+ Views

Let us take we have to eliminate the line breaks, space and tab space from the below string.eliminate.jpgExampleWe can make use of Replace() extension method of string to do it. Live Demousing System; namespace DemoApplication {    class Program {       static void Main(string[] args) {          string testString = "Hello \r beautiful \t world";          string replacedValue = testString.Replace("\r", "_").Replace("\t", "_");          Console.WriteLine(replacedValue);          Console.ReadLine();       }    } }OutputThe output of the above code isHello _ beautiful _ worldExampleWe can also make use ... Read More

Update Values of a Collection Using LINQ in C#

Nizamuddin Siddiqui
Updated on 08-Aug-2020 11:19:03

13K+ Views

If the collection is a List, then we can make use of ForEach extension method which is available as part of LINQ.Example Live Demousing System; using System.Collections.Generic; namespace DemoApplication {    class Program {       static void Main(string[] args) {          List fruits = new List {             new Fruit {                Name = "Apple",                Size = "Small"             },             new Fruit {         ... Read More

Difference Between int.Parse and Convert.ToInt32 in C#

Nizamuddin Siddiqui
Updated on 08-Aug-2020 11:14:17

6K+ Views

Convert a string representation of number to an integer, using the int.Parse or Convert.ToInt32 method in C#. If the string cannot be converted, then the int.Parse or Convert.ToInt32 method returns an exceptionConvert.ToInt32 allows null value, it doesn't throw any errors Int.parse does not allow null value, and it throws an ArgumentNullException error.Example Live Democlass Program {    static void Main() {       int res;       string myStr = "5000";       res = int.Parse(myStr);       Console.WriteLine("Converting String is a numeric representation: " + res);       Console.ReadLine();    } }OutputConverting String is a ... Read More

Delete All Files and Folders from a Path in C#

Nizamuddin Siddiqui
Updated on 08-Aug-2020 11:12:07

4K+ Views

For deleting all the folders and its respective directories we can make us System.IO namespace available in C#. The DirectoryInfo() class provides the details of all sub directories and file in a directory.ExampleLet us consider a directory Demo having two sub directories and has some files like below.using System.IO; namespace DemoApplication {    class Program {       static void Main(string[] args) {          DirectoryInfo di = new DirectoryInfo(@"D:\Demo");          foreach (DirectoryInfo dir in di.GetDirectories()) {             foreach (FileInfo file in dir.GetFiles()) {           ... Read More

Advertisements