Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Writing to a text file really inneficient [C#]

I'm using this code to write to a text file using many threads at the same time. (Parallel.Foreach a list) with a high thread count.

However, I've noticed that multi thread doesn't increase the speed from a normal foreach when using this method.

PHP Code

<?php 
public static void WriteDebug(string pathFile, string text)
 
           {
 
               try
                
{
 
                   locker.AcquireWriterLock(int.MaxValue);
 
                   File.AppendAllText(pathFile, text + Environment.NewLine);
 
               }
 
               finally
                
{
 
                   locker.ReleaseWriterLock();
 
               }
 
           } 

Is there any other efficient way to output a lot of data to somewhere (preferably to a file)?

I need it to be super super quick.

Cheers
(Apr 22, 2020 01:49 PM)draxyee Wrote:  I'm using this code to write to a text file using many threads at the same time. (Parallel.Foreach a list) with a high thread count.

However, I've noticed that multi thread doesn't increase the speed from a normal foreach when using this method.

PHP Code

<?php 
public static void WriteDebug(string pathFile, string text)
 
           {
 
               try
                
{
 
                   locker.AcquireWriterLock(int.MaxValue);
 
                   File.AppendAllText(pathFile, text + Environment.NewLine);
 
               }
 
               finally
                
{
 
                   locker.ReleaseWriterLock();
 
               }
 
           } 

Is there any other efficient way to output a lot of data to somewhere (preferably to a file)?

I need it to be super super quick.

Cheers

try using streamwriter it might help you
I would strongly advise log4net for this


Code
class Program
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        static void Main(string[] args)
        {
            log.Info("Hello logging world!");
            Console.WriteLine("Hit enter");
            Console.ReadLine();
        }
    }

Have a look here > https://stackify.com/log4net-guide-dotnet-logging/
static Streamwriter text = new Steamwriter("file.txt", true);

That line in top of main.

Inside main:
text.Autoflush = true;
text.WriteLine(string);
[Image: ME3mSDm.gif]
(Apr 23, 2020 08:13 AM)Sickness Wrote:  static Streamwriter text = new Steamwriter("file.txt", true);

That line in top of main.

Inside main:
text.Autoflush = true;
text.WriteLine(string);

By default, a StreamWriter is not thread safe.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Shell] by memoryNov 20, 2020 11:47 PM 6 279 Nov 20, 2020 11:47 PM
Last Post: memory
[Java] by GrandpaSwampDec 14, 2019 09:09 PM 5 258 Dec 14, 2019 09:09 PM
Last Post: GenericSkid
[C] by pebblakOct 15, 2019 05:24 AM 6 307 Oct 15, 2019 05:24 AM
Last Post: Joseph Ducreux