I haven't changed the performance aspect much, but I have nonetheless made lots of improvements with regard to syntax, readability, portability and conformity to modern C coding standards:
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
// Compile with -std=c11 (or -std=c99)
// Use uppercase for constants
#define LINHAS 1024
#define VEZES 5120
// If you're going to hardcode your filenames, better make them macros too.
#define FICHEIROS {"SO2014-0.txt", \
"SO2014-1.txt", \
"SO2014-2.txt", \
"SO2014-3.txt", \
"SO2014-4.txt"}
#define CADEIAS {"aaaaaaaaa\n", \
"bbbbbbbbb\n", \
"ccccccccc\n", \
"ddddddddd\n", \
"eeeeeeeee\n", \
"fffffffff\n", \
"ggggggggg\n", \
"hhhhhhhhh\n", \
"iiiiiiiii\n", \
"jjjjjjjjj\n"}
#define CAD_SIZE 10
// Convenient macro for whenever you need to loop through a static size array.
// Calculated at compile time.
#define ELEMENT_C(a) (sizeof(a) / sizeof((a)[0]))
// Always include prototypes of all your functions.
// Try to use English for variables and function names as much as possible.
// I'm not sure what they mean, so I haven't translated them here though.
int main(void);
int random(const int range);
void escreve(const int fd, const char *cad);
// Functions without arguments should have the word "void" between parentheses.
int main(void) {
// time_t is not guaranteed to be unsigned (I think), so cast explicitly
// for portability.
srand((unsigned) time(NULL));
// No need to specify an array size. The compiler will count it by itself.
const char *ficheiros[] = FICHEIROS,
*cadeias[] = CADEIAS;
// Open all files before writing anything. Your code calls open() for every
// call to escreve(). Seems like a bad idea to me.
int fds[ELEMENT_C(ficheiros)];
// Being able to declare variables inside loops is a nice feature of C99
// onward. In general, tight scoping can prevent many kinds of bugs and
// makes debugging easier.
for (unsigned i = 0; i < ELEMENT_C(ficheiros); i++) {
fds[i] = open(ficheiros[i], O_WRONLY | O_CREAT, S_IRWXU | S_IROTH);
// Always check the return value of library functions for errors.
if (fds[i] == -1) {
// Use strerror() or perror() to get useful debug information
fprintf(stderr, "Failed to open() %s: %s\n", ficheiros[i],
strerror(errno));
// Use these stdlib macros for more readable return values.
return EXIT_FAILURE;
}
}
for (int i = 0; i < VEZES; i++) {
escreve(fds[random(ELEMENT_C(ficheiros))],
cadeias[random(ELEMENT_C(cadeias))]);
}
// Close all files after writing everything.
for (unsigned i = 0; i < ELEMENT_C(ficheiros); i++) {
if (close(fds[i]) == -1) {
fprintf(stderr, "Failed to close() %s: %s\n", ficheiros[i],
strerror(errno));
return EXIT_FAILURE;
}
}
// Don't forget to return your exit code at the end of main.
return EXIT_SUCCESS;
}
int random(const int range) {
// This is the proper way to use rand().
// Your version suffers from rounding errors.
return rand() / (double) (RAND_MAX + 1) * range;
}
void escreve(const int fd, const char *cad) {
for (int i = 0; i < LINHAS; i++) {
if (write(fd, cad, CAD_SIZE) == -1) {
fprintf(stderr, "Failed to write() %s to file descriptor %d: %s\n",
cad, fd, strerror(errno));
exit(EXIT_FAILURE);
}
}
}
ficheiros
->file_names
– JaDogg Oct 6 '14 at 2:54