Optimization (ITI8510): erinevus redaktsioonide vahel

Allikas: Lambda
Mine navigeerimisribale Mine otsikasti
Resümee puudub
Resümee puudub
1. rida: 1. rida:
{{iti8510}}''Harjutame programmi optimeerimist''
{{iti8510}}''Optimization of a program''


== Programm ==
== Programm ==


Kirjutage programm, mis loeb sisse faili ning väljastab selle faili statistika.
Write a program which will read a file and outputs its statistics.


* "Standardse" sisendfaili saamiseks jooksutage:
* For a "standard" input file with random content, please run:
  dd if=/dev/urandom of=random_file count=1048576 bs=1
  dd if=/dev/urandom of=random_file count=1048576 bs=1


* Programmi väljundiks on 256 (inimloetavat) rida ASCII numbreid, mis kajastavad faili sisu. Esimesel real oleks baidi väärtusega 0x0 esinemiskordade arv, real 256 baidi 0xff esinemiskordade arv.
* The output of the program should be 256 human-readable lines of ASCII numbers, which represent the content of the file. The first line should contain the number of occurences of byte with a value of
* Programmi käivitumise aega mõõtke käsuga:
0x0 (or binary zero) in the file and the last line the number of occurences of byte with value 0xff (or 256 in decimal).
  time programminimi
* Measure the running time of the program with:
  time programname


== Vihjeid ==
== Hints ==


* Vaadake kas saate mmap() abil faili lugemist kiirendada.
* Can you speed up the reading of the file with mmap()?
* Vaadake kas on kasu loop unroll trikist
* Does the loop unrolling trick work?
* Võib-olla soovite thread-e või paralleelprogramme proovida?
* Perhaps it is possible to use parallel programming or threads?
* Optimeerige programm -O võtmega
* Does compilation optimization with -O work?
* Väljundi kirjutamisel on võib-olla mõistlik printf asemel kirjutada oma väljundfunktsioon
* Can you replace printf with something faster that you write yourself?
* Võib-olla on kasu parameetrist inline.
* Perhaps the keyword inline can be used.


== Example code for base program ==
== Example code for base program ==

Redaktsioon: 4. november 2015, kell 09:32

Real-Time Operating Systems and Systems Programming
[ @ ]

Optimization of a program

Programm

Write a program which will read a file and outputs its statistics.

  • For a "standard" input file with random content, please run:
dd if=/dev/urandom of=random_file count=1048576 bs=1
  • The output of the program should be 256 human-readable lines of ASCII numbers, which represent the content of the file. The first line should contain the number of occurences of byte with a value of

0x0 (or binary zero) in the file and the last line the number of occurences of byte with value 0xff (or 256 in decimal).

  • Measure the running time of the program with:
time programname

Hints

  • Can you speed up the reading of the file with mmap()?
  • Does the loop unrolling trick work?
  • Perhaps it is possible to use parallel programming or threads?
  • Does compilation optimization with -O work?
  • Can you replace printf with something faster that you write yourself?
  • Perhaps the keyword inline can be used.

Example code for base program

#include <stdlib.h>
#include <stdio.h>

long int stats[256];

int main() {
        int i, c;
       FILE * f; 

        f = fopen("random_file", "r");
        if(f == NULL) {
                perror("slowstat");
                exit(EXIT_FAILURE);
       } 

        while((c = fgetc(f)) != EOF) {
                stats[c]++;
       } 

        for(i = 0; i < 255; i++) {
                printf("%ld\n", stats[i]);
        }
        exit(EXIT_SUCCESS);
}