In the simple example you specified, there is no difference in the generated bytecode, and therefore, there is no difference in performance. It would also not create a noticeable difference in compilation speed.
In more complex code (probably involving implicits), you may encounter situations where compilation type performance will improve markedly by specifying some types. However, I would completely ignore this until you come across it - specify types or not for other, better reasons.
According to your question, there is one very important case when it is recommended to specify the type in order to ensure good performance at runtime. Consider this code:
val x = new AnyRef { def sayHi() = println("Howdy!") } x.sayHi
This code uses reflection to invoke sayHi, and that is a huge performance hit. Recent versions of Scala will warn you about this code for this reason, if you did not enable the language function for it:
warning: reflective access of structural type member method sayHi should be enabled by making the implicit value scala.language.reflectiveCalls visible. This can be achieved by adding the import clause 'import scala.language.reflectiveCalls' or by setting the compiler option -language:reflectiveCalls. See the Scala docs for value scala.language.reflectiveCalls for a discussion why the feature should be explicitly enabled.
Then you can change the code to this one that does not use reflection:
trait Talkative extends AnyRef { def sayHi(): Unit } val x = new Talkative { def sayHi() = println("Howdy!") } x.sayHi
For this reason, you usually want to specify the type of a variable when you define classes this way; Thus, if you inadvertently add a method that requires reflection to be called, you will get a compilation error - the method will not be defined for the type of the variable. Thus, although it is not that type specifying makes the code run faster, in this case, if the code is slow, type specifying makes compilation impossible.
val x: AnyRef = new AnyRef { def sayHi() = println("Howdy!") } x.sayHi // ERROR: sayHi is not defined on AnyRef
There are, of course, other reasons why you might need to specify a type. They are necessary for the formal parameters of methods / functions and for return types of recursive or overloaded methods.
In addition, you should always specify return types for methods in the public API (unless they are simply trivially obvious), or you can use different method signatures than you planned, and then risk compromising existing clients of your API when you correct the signature.
Of course, you can intentionally expand the type so that you can later assign other types of objects, for example,
var shape: Shape = new Circle(1.0) shape = new Square(1.0)
But in these cases, the impact of performance is not affected.
It is also possible that specifying the type will lead to the conversion, and of course, this will have a performance impact that affects the conversion.