Arduino processing/communication speed

Hello,
I'm not an advance player in Arduino game, so would be happy for dumb explanations for my might-be-silly questions.
I am using arduino due.
I would like to retrieve/process/store data from my analog input(microphone) with external program - Matlab. As far as I understood, I need to use serial communication for this.
I also made simple tests to make sure my sampling frequency is high -- with only analogRead function, it says that I get ~250k samples per second, which is high, and more than enough for me. I need just 40kHz for audible sound's manipulations.
But! when I add serial.print, it ruins everything. It slows down to [what??] smth around 2.5kHz.
this is sample code, it's an easy one(and I hope I did it correctly). I'm using hightest possible for serial monitor baud rate - 115200.
the question is, if I increase baud to maximum(someone said it's possible to set 1Mhz), will I get higher sampling frequency? what does it depend on? =//
One more question: how am I supposed to finally enjoy all of Signal Processing MATlab features without losing frequency rate? at least 40kHz...
and one more question(attention, the most stupid one): Am I right, saying that the more functions/math expressions/ processings/lines I add within loop() function, less sampling rate I get?

void setup() {
  // put your setup code here, to run once:

 Serial.begin(115200);
 pinMode(A0, INPUT);
}
long startTime, endTime;

void loop() {
  startTime=millis();
  // put your main code here, to run repeatedly:
 
  for (int i=0; i<1000; i++){
  int senV=analogRead(A0);
  //Serial.println(senV);  
  }
  
  endTime=millis();
  long a=1000000/(endTime-startTime);
  Serial.print("a=");
  Serial.println(a);
}

omg, don't swear me please, and help =]]

As far as I understood, I need to use serial communication for this.

Since that is what Matlab understands how to do, yes.

But! when I add serial.print, it ruins everything. It slows down to [what??] smth around 2.5kHz.

Part of that is having to convert the value to a string. You can avoid that part by sending binary data.

if I increase baud to maximum(someone said it's possible to set 1Mhz), will I get higher sampling frequency?

Of course. But, does Matlab work at that speed?

One more question: how am I supposed to finally enjoy all of Signal Processing MATlab features without losing frequency rate? at least 40kHz...

What signal process are you doing?

Am I right, saying that the more functions/math expressions/ processings/lines I add within loop() function, less sampling rate I get?

Yes. Some functions/processing have more impact than others. Incrementing a variable is fast. Writing to an SD card is not. You can't just count statements to get an estimate of speed.

Thank you for directions, PaulS

What is the possible range of values in the variable "a" (a very unhelpful name for a variable)

If it is small it may be possible to convert it to something less than a long before sending it.

...R

Robin2:
What is the possible range of values in the variable "a" (a very unhelpful name for a variable)

emm, as far as I know, it's max value is the sampling speed of my arduino due.. so max I got is 333333.
may be it's a rubbish..

but I'm sending it just to check sampling frq with and without serial communication.

You might find this an interesting thread - Experiment - split up analog read in 3 steps - Libraries - Arduino Forum -

it explains how the analogRead can be split up to be used asynchronously.

Further definitely use binary data, and maybe (RLE, delta) compression on top of that.

messy089:
so max I got is 333333.

That does not tell us the range. We also need to know the minimum value.

...R