Maxximiliann

Maxximiliann

* (CompileError): undefined function

defmodule Benchmark do

	def main do
		module_function = Foo.main
		_ = module_function
	end

	def run(params) do
		number_of_trials = Enum.at(params, 0)
		total_warm_up_cycles = Enum.at(params, 1)

		IO.puts("Warming up . . . initiating first trial of #{number_of_trials}.")

		start_time = System.monotonic_time(:millisecond)

		main = main()
		_ = main

		end_time = System.monotonic_time(:millisecond)
		elapsed_time = (end_time - start_time) / 1000

		results = %{time: [elapsed_time], acc: 1}  

		run(results, number_of_trials, totaL_warm_up_cycles)
	end


	def run(results, number_of_trials, total_warm_up_cycles) do
		start_time = System.monotonic_time(:millisecond)

		main = main()
		_ = main

		end_time = System.monotonic_time(:millisecond)
		new_time = (end_time - start_time) / 1000 
		updated_acc = results.acc + 1

		average_time = 
		cond do 
			results.acc == total_warm_up_cycles -> new_time
			results.acc > total_warm_up_cycles -> Enum.sum(results.time) / (updated_acc - (total_warm_up_cycles + 1))
			results.acc < total_warm_up_cycles -> []
		end

		results = 
		if results.acc >= total_warm_up_cycles do
			Enum.map(results, fn _map ->
				%{
					time: List.insert_at(results.time, 0, new_time),
					acc: updated_acc,
					average_time: average_time
				}
			end)
			|> List.first
		else
			Enum.map(results, fn _map ->
				%{
					time: [],
					acc: updated_acc,
					average_time: 0
				}
			end)
			|> List.first
		end

		cond do 
			results.acc < (total_warm_up_cycles + 1) -> IO.puts("Warming up . . . Trial number #{results.acc} of #{number_of_trials}.")	
			run(module_function, results, number_of_trials, total_warm_up_cycles)

			results.acc == (total_warm_up_cycles + 1) -> IO.puts("Average execution time after #{results.acc} trials of #{number_of_trials}: #{Float.floor(results.average_time, 3)} seconds.")
			run(module_function, results, number_of_trials, total_warm_up_cycles)

			results.acc < number_of_trials -> IO.puts("Average execution time after #{results.acc} trials of #{number_of_trials}: #{Float.floor(results.average_time, 3)} seconds.")
			run(module_function, results, number_of_trials, total_warm_up_cycles)

			results.acc == number_of_trials -> IO.puts("Final trial - Average execution time: #{Float.floor(results.average_time, 3)} seconds.")
		end
	end		
	
end

This is designed to accept the following command:
iex(1)> Benchmark.run([20, 5])

Which would then output:

Warming up . . . initiating first trial of 20.
Warming up . . . Trial number 2 of 20.
Warming up . . . Trial number 3 of 20.
Warming up . . . Trial number 4 of 20.
Warming up . . . Trial number 5 of 20.
Average execution time after 6 trials of 20: 0.754 seconds.
Average execution time after 7 trials of 20: 0.754 seconds.
Average execution time after 8 trials of 20: 0.549 seconds.
Average execution time after 9 trials of 20: 0.48 seconds.
Average execution time after 10 trials of 20: 0.448 seconds.
Average execution time after 11 trials of 20: 0.496 seconds.
Average execution time after 12 trials of 20: 0.471 seconds.
Average execution time after 13 trials of 20: 0.499 seconds.
Average execution time after 14 trials of 20: 0.485 seconds.
Average execution time after 15 trials of 20: 0.47 seconds.
Average execution time after 16 trials of 20: 0.457 seconds.
Average execution time after 17 trials of 20: 0.447 seconds.
Average execution time after 18 trials of 20: 0.438 seconds.
Average execution time after 19 trials of 20: 0.431 seconds.
Final trial - Average execution time: 0.427 seconds.
:ok

When attempting to compile this module, however, it triggers the following error:

== Compilation error in file lib/Benchmark.ex ==
** (CompileError) lib/Benchmark.ex:24: undefined function totaL_warm_up_cycles/0

It looks like total_warm_up_cycles = Enum.at(params, 1) is being ignored? If so, why and how is this resolved?

Ideally I’d love to get this to run with something like iex(2)> Benchmark.run([MODULE.FUNCTION, 20, 5]) which would make this more dynamic since the particular module function to be benchmarked wouldn’t have to be hardcoded. After spending dozens of hours searching far and wide, though, I can’t find any resources that cover how exactly this is done. Is this even possible?

Thanks for all your help and opinions! :slight_smile:

Marked As Solved

Nicd

Nicd

totaL_warm_up_cycles has a big L, it’s a typo.

Also Liked

NobbZ

NobbZ

There is an uppercase L in the usage of the variable, but not in it’s definition

kokolegorille

kokolegorille

You put a capital L in the second totaL_warm_up_cycles.

Nicd

Nicd

As for this part, have you tried for example Benchmark.run(&Module.function/n, 20, 5) (substituting module, function, and n for the respective module, function, and arity)? Then you could call the function with function.(args) (note the period).

EDIT: For more information about function capturing, see: https://elixir-lang.org/getting-started/modules-and-functions.html#function-capturing

Nicd

Nicd

Well you would need to change the rest of your function code to match… :slight_smile: In my suggestion the first parameter would be the function to run, so you’d need to use another place for the results. Or put the function in another parameter.

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement