Thread: Is calculating for Arrays faster then .txt files ?

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    385

    Is calculating for Arrays faster then .txt files ?

    I wonder if calculations of arrays is faster compared to if you do calculations from a simple .txt file.
    I will give this example.

    (Calculation from a .txt file while reading it)
    The .txtfile has one line and look like this:

    1,2,3,4,5

    I will do a calculation right from the file while reading. I split the values and do:
    1*2*3*4*5 wich will give me: 120
    .................................................. ..........................

    Instead of the above approach I will already have these values stored and ready in an array called Test[4]
    The calculation will be:
    Test[0] * Test[1] * Test[2] * Test[3] * Test[4] wich will give me: 120


    Now I wonder if the calculations with the arrays is faster than while reading from a .txt file in realtime and doing the calculations.
    If it is, how much faster are we talking about here ?
    We could also refer to an example where we will use 1 million numbers instead of 5 as in our example wich will be closer to what I will do later.


    Why I wonder this is because I will use very large .txtfiles and do calculations with.
    Thanks...
    Last edited by Coding; 12-25-2007 at 06:57 PM.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    there are a lot of variables concerning arrays and text files to take into consideration, but i will attempt to give you my answer at the most general level. This is like comparing apples to oranges.

    There is a lot more overhead using the ifstream and ofstream libraries as opposed to performing a basic array calculations.

    Often, when a file is read, the text is stored into an array.

    Calculations on an array of primitive data types is faster than an array of an aggregate data type. Example, array calculations on an array of int's is faster than array calculations on an array of structs or classes.

    All of these answers are dependent on what type of calculations ye' want to do in the first place.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I'm not quite sure what you're trying to ask, but I'll take a shot in the dark at giving you some answers.

    Reading from RAM is over 1000 times faster than reading from a hard disk.

    You can't do any calculations using data on disk; you must load it into RAM, then the program will load it into cache and into CPU registers... So if your data is in a .txt file, load it into some variables once and use those variables for all your calculations (don't re-read the same .txt file over and over again).

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But it's much more efficient to read all data at once rather than nibble a little data at a time and do the calculations. So read all the data into an array and process it from there.
    If you don't have to read at all, you'll see huge improvements as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes thats what I thougt also, I will try to read in all data into arrays once and from here I will do all the calculations.
    This sounds like a good solution..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to write files more faster in C
    By spank in forum C Programming
    Replies: 75
    Last Post: 01-08-2008, 02:15 PM
  2. Saving large files faster than light!
    By ZapoTex in forum C Programming
    Replies: 19
    Last Post: 01-09-2005, 05:47 PM
  3. Replies: 8
    Last Post: 12-27-2003, 02:30 PM
  4. Printing list of .txt files saved in directory
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 01-16-2002, 12:33 AM
  5. displaying text files, wierd thing :(
    By Gades in forum C Programming
    Replies: 2
    Last Post: 11-20-2001, 05:18 PM