Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Which is faster?  (Read 2709 times)

0 Members and 1 Guest are viewing this topic.

Cl9

  • Newbie
  • *
  • Posts: 21
    • View Profile
    • Email
Which is faster?
« on: January 02, 2014, 08:19:35 pm »
I've got a question, which would be faster if I wanted to send a client its inventory which is made up of an array of integer item id's server wise:

Convert the integers into strings, and put them all into the same string seperated by commas, and then send the whole string in one packet to the client to be seperated back into individual item id's like so:

Code: [Select]
int Inventory[10] = {0,1,2,3,4,5,6,2,1};
*send each integer seperately*

or:

Code: [Select]
int Inventory[10] = {0,1,2,3,4,5,6,2,1};
*convert into strings*
string Result = "0,1,2,3,4,5,6,2,1";
*send in one packet instead of 10*

Which of these has the best performance?
Or is there a faster way?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Which is faster?
« Reply #1 on: January 02, 2014, 08:29:02 pm »
Don't send them separately, every packet has an overhead. But there's no need to use strings, have a look at the operator<< for sf::Packet -- you can concatenate integers directly.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Cl9

  • Newbie
  • *
  • Posts: 21
    • View Profile
    • Email
Re: Which is faster?
« Reply #2 on: January 02, 2014, 08:43:00 pm »
And what if you don't know how many things you'll have to send?
Such as a map, made up of tiles, which can be varying sizes?

Btw, great book on SFML :D
« Last Edit: January 02, 2014, 08:45:11 pm by Cl9 »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Which is faster?
« Reply #3 on: January 02, 2014, 09:40:40 pm »
And what if you don't know how many things you'll have to send?
Then you include an integer that stores the number of objects.

sf::Packet can be used in many ways, similar to binary files with std::fstream. The only thing you need is to define a format (or protocol in networking terms) that defines how the data is layed out. Concretely, this determines how the values are arranged inside a packet, and of what types they are. For example:
[MyEnum]  Packet type
[Uint32]  Number of objects
[float]   x position of 1st object
[float]   y position of 1st object
[Int32]   hitpoints of 1st object
[float]   x position of 2nd object
[float]   y position of 2nd object
[Int32]   hitpoints of 2nd object
...
You can also overload operator<< and operator>> for own types (such as an Object class in this example) to simplify the code.

Btw, great book on SFML :D
Thank you :)

By the way, if you need an example: Such a possible protocol is explained in the Network chapter of our book.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

JonDoe

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Which is faster?
« Reply #4 on: February 21, 2014, 10:53:28 am »
I have a similar question, I have a program that receives data, sent from a smarthphone, from a TCP/IP socket. I can not change the way the data is sent. The data is sent as a byte(char?) array and contains some floats, and some text. It looks something like
char[] = "4932942 ABC -0.123251515 0.12414   155.230420". I extract the floats from the string and process them in my program. The problem is that the size of this array can differ from sample to sample. So if I elect to use a byte array I end up with the array containing parts of more than one sample, which is unwanted because I want to receive one sample at a time, process it, and then receive the next one. The code I use to make sure I receive one message at a time is the following:

std::string sample = "";       
char data = '1';
std::size_t received;
while(data != '\n') {
        client.receive(&data, 1, received);
        sample += data;
}
 

According to Nexus' previous answer in this thread this way is inefficient. As I stated before, I can not control in any way how the data is sent. It would have been nice to just receive the sample as a package but I could not make it work since it is sent from, I believe, a Java program.

Is there an easy solution to my problem? That is, to receive each sample, and nothing more, with one call to the receive function?

 

anything