Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by benapprill · Jan 24, 2021 at 11:31 AM · dots

Accessing Time.DeltaTime inside scheduled Unity Job

I am trying to test job completion time within a system.


 using Unity.Entities;
 using UnityEngine;
 
 [DisableAutoCreation]
 public class SubSystemOne : SystemBase
 {
     float timer = 0f;
 
     protected override void OnCreate()
     {
         EntityManager.CreateEntity(ComponentType.ReadWrite<TestValueOne>());
     }
 
     protected override void OnUpdate()
     {
         Entities.ForEach((ref TestValueOne one) => {
             for (int i = 0; i < 10000; i++)
             {
                 //Throws an error if I schedule
                 timer += Time.DeltaTime;           
             }
         }).Schedule();
 
         Debug.Log(timer);
 
         World.GetExistingSystem<SimulationSystemGroup>()
             .RemoveSystemFromUpdateList(World.GetExistingSystem<SubSystemOne>());
         
     }
 }
 
 public struct TestValueOne : IComponentData
 {
     public float Value;
 }


This works fine if I just run the job. However, if I try to schedule it, I get an error. The error is because I am trying to access a reference value inside the scheduled job, which is understandable...


I have tried querying for the WorldTime component that exists by default in the World, but this does not seem to work...


I am wondering how best to proceed with completing a test like this. I am trying to set up multiple scheduled jobs to see their results. Maybe there is a better way to approach this kind of testing?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Jan 24, 2021 at 12:21 PM

Regardless of the fact that Time.deltaTime can only be accessed from the main thread, it makes absolutely no sense to do so unless you need a recent value for calculations on each object. The way you use it in your example code makes no sense. DeltaTime is the time between two frames on the main thread. You accumulate that time 10000 times, every object. What do you think this value would tell you in the end?


I haven't used ECS or the Job system yet, however if you want to measure time you probably should simply use the normal System.DateTime class to get the realtime. To see how long a certain process takes, you should just get the time when it's started and once again when it's finished. The difference between the two tells you how long it took. Though I'm not sure if that makes any sense with the Job system.


When looking at this you probably want to use OnStartRunning and OnStopRunning to measure time.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image benapprill · Jan 25, 2021 at 04:18 AM 0
Share

Thank you for the reply.
Because the job is scheduled, it will technically execute over multiple frames, correct? As it is not tied up on the main thread....
That is what I was assu$$anonymous$$g anyway. The idea was to use Time.DeltaTime to track how long the job took to complete. With that, I was gonna track completion time on multiple jobs running at the same time.
This may be something that is easily observed using the Profiler... $$anonymous$$aybe I will consider that.

avatar image
0

Answer by andrew-lukasik · Jan 25, 2021 at 10:46 AM

Q:

I am trying to test job completion time within a system.

A:

 protected override void OnUpdate ()
 {
     int numRepeats = 100000;
     
     var watch_1 = System.Diagnostics.Stopwatch.StartNew();
     Job
         .WithCode( () =>
         {
             float4 value = new float4{ x=2 , y=2 , z=2 , w=2 };
             for( int i=0; i<numRepeats ; i++ )
                 value = math.pow( value , i );
         } )
         .WithoutBurst().Run();
     UnityEngine.Debug.Log($"(NO BURST) Test code took {watch_1.Elapsed.TotalMilliseconds} [ms] to execute");
 
     var watch_2 = System.Diagnostics.Stopwatch.StartNew();
     Job
         .WithCode( () =>
         {
             float4 value = new float4{ x=2 , y=2 , z=2 , w=2 };
             for( int i=0; i<numRepeats ; i++ )
                 value = math.pow( value , i );
         } )
         .WithBurst().Run();
     UnityEngine.Debug.Log($"(WITH BURST) Test code took {watch_2.Elapsed.TotalMilliseconds} [ms] to execute");
 
     Enabled = false;
 }

Note: Profiler window prints all the scheduling and timing details you need.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

114 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[ECS] Create Entities from Job ( IJobForEachWithEntity) using an EntityCommandBuffer 1 Answer

Best way to get data from a Monobheaviour in a JobComponentSystem? 2 Answers

[ECS] DefaultWorldInitialization.Initialize take 50MB 1 Answer

Unity (ECS & DOTS) Entity Sinking 0 Answers

How to create realistic wire 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges