Optimization (ITI8510): erinevus redaktsioonide vahel
Mine navigeerimisribale
Mine otsikasti
Resümee puudub |
Resümee puudub |
||
| (ei näidata sama kasutaja 5 vahepealset redaktsiooni) | |||
| 1. rida: | 1. rida: | ||
{{iti8510}}'' | {{iti8510}}''Optimization of a program'' (Also available in [http://lambda.ee/w/index.php?title=Optimization_(ITI8510)&oldid=12060 Estonian]) | ||
== Programm == | == 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 | 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 | time programname | ||
== | == Hints == | ||
* | * 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. | ||
* | * (Sadly mmap for file reading fails in dijkstra) Can you speed up the reading of the file with mmap()? (Note -- mmap not yet explained in the lecture) | ||
== Example code for base program == | == Example code for base program == | ||
Viimane redaktsioon: 24. november 2017, kell 11:59
| Real-Time Operating Systems and Systems Programming |
| [ @ ] |
Optimization of a program (Also available in Estonian)
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
- 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.
- (Sadly mmap for file reading fails in dijkstra) Can you speed up the reading of the file with mmap()? (Note -- mmap not yet explained in the lecture)
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);
}