C ++ string quick output

I have a program that outputs data from an FPGA. Since changing EXTREMELY data quickly, I try to increase the speed of the program. Now I am printing data like this

for (int i = 0; i < 100; i++) { printf("data: %d\n",getData(i)); } 

I found that using one printf significantly increases speed

 printf("data: %d \n data: %d \n data: %d \n",getData(1),getData(2),getData(3)); 

However, as you can see, this is very dirty, and I cannot use the for loop. At first I tried to combine the lines using sprintf and then print everything at once, but this is as slow as the first method. Any suggestions?

Edit: First I print the file because I realized that console scrolling would be a problem. But its still too slow. I am debugging a memory controller for an external FPGA, so the closer to real speed, the better.

+4
source share
5 answers

Wow, I can’t believe that I didn’t do this before.

 int size = 100; char data[size]; for (int i = 0; i < size; i++) { *(data + i) = getData(i); } for (int i = 0; i < size; i++) { printf("data: %d\n",*(data + i)); } 

As I said, printf was a bottleneck, and sprintf did not improve much either. So I decided to avoid any kind of printing to the very end and use pointers instead

0
source

If you write to stdout, you may not be able to influence all of this.

Otherwise, set up buffering.

Now, Boost Karma is known to be pretty good. However, I will need to know more about your input.

Meanwhile, try manually saving your recordings: Live on Coliru

 #include <stdio.h> int getData(int i) { return i; } int main() { char buf[100*24]; // or some other nice, large enough size char* const last = buf+sizeof(buf); char* out = buf; for (int i = 0; i < 100; i++) { out += snprintf(out, last-out, "data: %d\n", getData(i)); } *out = '\0'; printf("%s", buf); } 
+6
source

How much data? Keep it in your memory until you are done, and then print it. Also, file output may be faster. Depending on the terminal, your program may block recording. Instead, you can choose to record and write directly to STDOUT.

in principle, you cannot do many synchronous I / O terminals on something where you want consistent, predictable performance.

+1
source

Try typing \r at the end of the line instead of the usual \n - if that works on your system. This way you are not getting continuous scrolling.

It depends on your environment if it works. And, of course, you cannot read all the data if they change very quickly.

Have you considered printing only every nth record?

0
source

I suggest you format the text to a buffer, and then use the fwrite function to write the buffer.

After creating the dasblinkenlight response, use fwrite instead of puts . The puts function searches for a terminating null character. The fwrite function writes as-is to the console.

 char buf[] = "data: 0000000000\r\n"; for (int i = 0; i < 100; i++) { // int portion starts at position 6 itoa(getData(i), &buf[6], 10); // The -1 is because we don't want to write the nul character. fwrite(buf, 1, sizeof(buf) - 1, stdout); } 

You might want to read all the data into a separate raw data buffer, then format the raw data into a “formatted” data buffer, and finally explode the entire “formatted” data buffer using a single fwrite call.

You want to minimize calls for sending data because there is overhead. The fwrite function has the same overhead record for writing 1 character, since it writes 10,000 characters. This is where buffering takes place. Using 1024 element buffers means that you use 1 function call to write 1024 elements, compared with 1024 calls writing one element each. The latter is 1023 additional function calls.

0
source

Source: https://habr.com/ru/post/1498669/


All Articles