user8268: I like having serial communication for debugging and testing purposes but after a while it takes away too much speed from the sketch. 
Is it possible to have the Arduino ignore serial.print and serial.println throughout my code, without turning it into a comment or placing every serial printing inside for example "if(debug == true)" statements?
Thanks in advance.

user13432: Yet another solution is to implement a dummy Serial device. That would be a class that as HardwareSerial inherits from Stream and implements the necessary virtual member functions with dummy functions. 
class NullSerial : public Stream {
public:
  virtual size_t write(uint8_t) { return (1); }
  virtual int available() { return (0); }
  virtual int read() { return (0); }
  virtual int peek() { return (0); }
  virtual void flush() {}
  ...
  void begin(unsigned long, uint8_t) {}
  void end() {}
  ...
};

NullSerial Serial;

As Serial is not defined as a weak symbol the application would need to use a Steam variable for output. And bind this to the HardwareSerial or the NullSerial.
#if defined(DEBUG)
Stream& trace = Serial;
#else
NullSerial noSerial;
Stream& trace = noSerial;
#else

#endif
...
trace.print(42);

This is not complete as all the HardwareSerial member functions (that the sketch uses) are needed but gives the general idea how to use OOP to solve the problem. 
Cheers!

user1323: You could, for example, use the preprocessor to change all Serial in your code.
#ifndef ENABLE_PRINT
// disable Serial output
#define Serial SomeOtherwiseUnusedName
static class {
public:
    void begin(...) {}
    void print(...) {}
    void println(...) {}
} Serial;
#endif


user1677: If you insist on top performance, the best thing would be to use a macro for that:
#define Sprintln(a) (Serial.println(a))

Then instead of 
Serial.println(F("Hello world!"));

write 
Sprintln(F("Hello world!"));

etc. To deactivate the Serial printing, define the macro empty:
#define Sprintln(a) 

This will have the preprocessor remove all debugging code defined with Sprintln from your code.
(Of course, there's a huge number of variations on this theme.)

8268: <p>I like having serial communication for debugging and testing purposes but after a while it takes away too much speed from the sketch. </p>

<p>Is it possible to have the Arduino ignore serial.print and serial.println throughout my code, without turning it into a comment or placing every serial printing inside for example "if(debug == true)" statements?</p>

<p>Thanks in advance.</p>
 13432: <p>Yet another solution is to implement a dummy Serial device. That would be a class that as HardwareSerial inherits from Stream and implements the necessary virtual member functions with dummy functions. </p>

<pre><code>class NullSerial : public Stream {
public:
  virtual size_t write(uint8_t) { return (1); }
  virtual int available() { return (0); }
  virtual int read() { return (0); }
  virtual int peek() { return (0); }
  virtual void flush() {}
  ...
  void begin(unsigned long, uint8_t) {}
  void end() {}
  ...
};

NullSerial Serial;
</code></pre>

<p>As Serial is not defined as a weak symbol the application would need to use a Steam variable for output. And bind this to the HardwareSerial or the NullSerial.</p>

<pre><code>#if defined(DEBUG)
Stream&amp; trace = Serial;
#else
NullSerial noSerial;
Stream&amp; trace = noSerial;
#else

#endif
...
trace.print(42);
</code></pre>

<p>This is not complete as all the HardwareSerial member functions (that the sketch uses) are needed but gives the general idea how to use OOP to solve the problem. </p>

<p>Cheers!</p>
 1323: <p>You could, for example, use the preprocessor to change all <code>Serial</code> in your code.</p>

<pre><code>#ifndef ENABLE_PRINT
// disable Serial output
#define Serial SomeOtherwiseUnusedName
static class {
public:
    void begin(...) {}
    void print(...) {}
    void println(...) {}
} Serial;
#endif
</code></pre>
 1677: <p>If you insist on top performance, the best thing would be to use a macro for that:</p>

<pre><code>#define Sprintln(a) (Serial.println(a))
</code></pre>

<p>Then instead of </p>

<pre><code>Serial.println(F("Hello world!"));
</code></pre>

<p>write </p>

<pre><code>Sprintln(F("Hello world!"));
</code></pre>

<p>etc. To deactivate the <code>Serial</code> printing, define the macro empty:</p>

<pre><code>#define Sprintln(a) 
</code></pre>

<p>This will have the preprocessor remove all debugging code defined with <code>Sprintln</code> from your code.</p>

<p>(Of course, there's a huge number of variations on this theme.)</p>
